开发者

Extracting Usable Info From datetime field in php/MySQL

I am trying to format a datetime variable with the following code:

$passed_time = $stu_quiz->c_date_time;
$passed_time_string = date_format($passed_time, 'M-d-Y');

For some reason, if I print $passed_time_string, the output is blank, but if I print out $passed_time, I get the date (in the format开发者_StackOverflow 2011-06-15 21:43:09).

Why is the date_format method not working?


The date_format function expects a "DateTime" object that is created using date_create.

Example:

$passed_time = date_create($stu_quiz->c_date_time);
$passed_time_string = date_format($passed_time, 'M-d-Y');


You are looking for just date() http://php.net/manual/en/function.date.php


If you don't have PHP 5.3 and cannot use the DateTime class, try this:

$passed_time_string = date("M-d-Y", strtotime($passed_time));

First it converts your original MySQL time to a unix timestamp, then formats it as M-d-Y

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜