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

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


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