Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

<?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

like image 419
iamjonesy Avatar asked Dec 08 '22 00:12

iamjonesy


2 Answers

Try:

echo date ("d/m/Y h:ia",strtotime($row->created));
like image 80
profitphp Avatar answered Dec 10 '22 13:12

profitphp


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.

like image 26
treeface Avatar answered Dec 10 '22 12:12

treeface