nanny boy nanny boy - 23 days ago 7
PHP Question

php datetime output issue

I fetches datetime in the db from different column based on condition:

script in PHP:

if($obj->acct_status == 2)
$dt = date('d M, Y h:i:s A', strtotime($obj->approve_datetime));

if($obj->acct_status == 4)
$dt = date('d M, Y h:i:s A', strtotime($obj->suspend_datetime));


the db stored datetime as below format:

2016-11-11 14:26:03


but the output is:

30 Nov, -0001 12:00:00 AM


What's causes this issue?

Answer

I strongly believe strtotime() should be avoided when there is a format available.

$from_format = 'Y-m-d H:i:s';
$to_format   = 'd M, Y h:i:s A';
$date = '2016-11-11 14:26:03';        


echo DateTime::createFromFormat($from_format, $date)->format($to_format);

Outputs:

11 Nov, 2016 02:26:03 PM
Comments