开发者

CodeIgniter: format mysql DATETIME field dd/mm/yy hh:mm

Hi pretty much what it says on the tin.

I have a datetime mysql field I want to output in the format dd/mm/yyyy hh:mm like 07/01/2011 22:16.

I've tried开发者_StackOverflow中文版:

<?php 
  $datestring = '%d/%m/%Y %h:%i';  
  echo mdate($datestring,$row->created); 
?>

But I'm getting an error:

Message: A non well formed numeric value encountered

Any help most appreciated!

Cheers,

Billy


Try:

echo date ("d/m/Y h:ia",strtotime($row->created));


The second parameter of the mdate() function still needs to be an integer timestamp, just like the native PHP date() function. Try using the strtodate() function which accepts a string as a parameter (including the MySQL date format) and returns an integer. This can be done like this:

$datestring = '%d/%m/%Y %h:%i';
echo mdate($datestring, strtodate($row->created));

The only difference between mdate() and date() is, as the CodeIgniter docs say:

This function is identical to PHPs date() function, except that it lets you use MySQL style date codes, where each code letter is preceded with a percent sign: %Y %m %d etc.

The benefit of doing dates this way is that you don't have to worry about escaping any characters that are not date codes, as you would normally have to do with the date() function.


Got this to work using treeface's solution, with one minor change:

$datestring = '%d/%m/%Y %h:%i';
echo mdate($datestring, strtoDATE($row->created)); 
    //strtoDATE didn't work but strtoTIME did

Had me scratching my head for hours, but now it works, I'm able to keep using CI helper for all date functions.

HTH


I'm using:

mdate(date_string,mysql_to_unix($row->created))

That should work.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜