Yii Framework Forum: Datetime Format From Mysql? - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Datetime Format From Mysql? Rate Topic: -----

#1 User is offline   clonevn 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 111
  • Joined: 25-March 13

Posted 30 April 2013 - 02:04 PM

Hi, I've recognized that the dateTime type in mySQL is yyyy-mm-dd hh:mm:ss. I want to convert it to other format that allows me to compare it with the current date or the date format variable but Im still stuck in here. Also, can I separate "yyyy-mm-dd hh:mm:ss" to "yyyy-mm-dd" and "hh:mm:ss"? Because I want also compare the time from this data with the time that I set up. Your help would be advance, thank
0

#2 User is offline   Fabrizio Caldarelli 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 283
  • Joined: 21-March 13
  • Location:Rome, Italy

Posted 30 April 2013 - 03:12 PM

You can use Yii class helper CDateTimeParser (http://www.yiiframew...CDateTimeParser)
or date's php function (http://php.net/manua...nction.date.php)
0

#3 User is offline   clonevn 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 111
  • Joined: 25-March 13

Posted 30 April 2013 - 07:44 PM

View PostFabrizio Caldarelli, on 30 April 2013 - 03:12 PM, said:

You can use Yii class helper CDateTimeParser (http://www.yiiframew...CDateTimeParser)
or date's php function (http://php.net/manua...nction.date.php)

Thank, but CDataTimeParser only give me the date, it does not give me the time: "hh:mm:ss". Does Yii helper have a class that give only the time from datatime formmat?
0

#4 User is offline   Fabrizio Caldarelli 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 283
  • Joined: 21-March 13
  • Location:Rome, Italy

Posted 01 May 2013 - 02:42 AM

If you read CDateTimeParser there is at bottom the CDateFormatter link at reference, that convert timestamp to string.

In general you can sue date's php function:

String (yyyy-mm-dd hh:mm:ss) to timestamp: strtotime( $var )
Date to string: date('h:i', $timestamp)
0

#5 User is offline   le_top 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 302
  • Joined: 08-June 10
  • Location:France

Posted 01 May 2013 - 03:24 AM

Hi

I have the following method as a utility function (in a class 'Utils'). I use it whenever I have a datetime value that is supposed to be in "database format" and it therefore also checks if the value is a CDbExpression representing 'NOW()'.


Another thing to take care of is timezones. I have mysql and the application running in 'UTC' and adjust times to user space times when needed. So the method here does nothing with timezones because I take care of that elsewhere.

    /**
 	* Convert datetime (from/to database) to unix time.
 	*
 	* Returns null if source value is null.
 	* Returns $datetime paramter if it is an integer value (so already a time value).
 	* Returns current time if parameter is CDbExpression "NOW()".
 	*
 	* @param integer|string|CDbExpression $datetime
 	* @throws Exception  If CDbExpression is not "NOW()" or DateTime is not an expected format.
 	* @return NULL|number Null or timestamp value
 	*/
    static function convertDbDateTimeToTime($datetime=null) {
        if($datetime===null) {
            return null;
        } elseif($datetime instanceof CDbExpression) {
            /* @var $datetime CDbExpression */
            if(strtoupper(trim($datetime->expression))==="NOW()") {
                return time();
            } else {
                throw new Exception(Yii::t('app','Unknown CDbExpression "{expression}"',array("{expression}"=>$datetime->expression)));
            }
        } elseif(!is_numeric($datetime)) {
            $stamp= DateTime::createFromFormat("Y-m-d H:i:s", $datetime);
            if($stamp) {
                return $stamp->getTimestamp();
            } else {
                throw new Exception(Yii::t('app','Bad timestamp "{stamp}"',array("{stamp}"=>$datetime)));
            }
            return $stamp;
        }
        return $datetime;
    }


0

#6 User is offline   clonevn 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 111
  • Joined: 25-March 13

Posted 01 May 2013 - 10:08 AM

View PostFabrizio Caldarelli, on 01 May 2013 - 02:42 AM, said:

If you read CDateTimeParser there is at bottom the CDateFormatter link at reference, that convert timestamp to string.

Thank so much, it worked perfectly :
Yii::app()->dateFormatter->formatDateTime(CDateTimeParser::parse($model->startdate, 'yyyy-MM-dd hh:mm:ss'),null,'medium');

And the output is : Wednesday, May 1, 2013. However, I would like to take only the day of the date, which means the return is "Wednesday". But I could not find it in the Date time format patterns. The best thing closest "Wednesday" to is "EEE, MMM d, ''yy ==> Wed, July 10, '96".
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users