Datetime Format From Mysql?

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

You can use Yii class helper CDateTimeParser (http://www.yiiframework.com/doc/api/1.1/CDateTimeParser)

or date’s php function (http://php.net/manual/it/function.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?

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)

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;

    }



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”.