nanny boy nanny boy - 1 year ago 77
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 Source

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);


11 Nov, 2016 02:26:03 PM
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download