Question

I tried converting a 23 hour format in format m-d-y h:i which is march 1st 2014 1 am

03-01-2014 01:00 am

with

date_format('03-01-2014 01:00 am','Y-m-d h:i:s')
date('Y-m-d h:i:s',strtotime('03-01-2014 01:00 am')

to its equivalent 24hours format Y-m-d H:i:s but i get wrong result

2014-01-03 01:00:00

it was suppose to give

2014-03-01 01:00:00
Was it helpful?

Solution

03-01-2014 01:00 am is ambiguous. Is it March 1st? Or April 3rd? From the manual:

Dates in the m/d/y or d-m-y formats are disambiguated by looking at the separator between the various components: if the separator is a slash (/), then the American m/d/y is assumed; whereas if the separator is a dash (-) or a dot (.), then the European d-m-y format is assumed.

To avoid potential ambiguity, it's best to use ISO 8601 (YYYY-MM-DD) dates or DateTime::createFromFormat() when possible.

$date = DateTime::createFromFormat('m-d-Y h:i a', '03-01-2014 01:00 am');
echo $date->format('Y-m-d H:i:s');

OTHER TIPS

For 2005-01-30 11:03 pm use

date('Y-m-d h:i a');

For 2005-01-30 23:03 use

date('Y-m-d H:i');
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top