14

Quite simply in PHP I have a date of 8th January 2011, in the format 08-01-11 - when I run this into strtotime and convert it back into a different date format, it reverts back to 1st August 2011 - not ideal!

Is there any easy way around this, rather than having to place everything into different arrays/variables and back again?

Thank you!

Nick
  • 1,233
  • 13
  • 26
  • 37

3 Answers3

31

The perfect solution would be for the US to use the correct date format in the first place... ;0)

I do this to get around it:

$date = "31/12/2012";
$bits = explode('/',$date);
$date = $bits[1].'/'.$bits[0].'/'.$bits[2];

$date is now strtotimeable

myshadowself
  • 445
  • 1
  • 4
  • 8
19

From the PHP 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."

I replaced slashes with dashes and then strtotime worked as expected for UK dates.

Community
  • 1
  • 1
gregmacoy
  • 191
  • 1
  • 2
11

strtotime() works with US dates only:

The function expects to be given a string containing a US English date format and will try to parse that format into a Unix timestamp.

You would have to either rearrange the date format or use date_parse_from_format() (PHP 5.3+) to parse the UK style string.

Liam McArthur
  • 1,033
  • 3
  • 18
  • 42
Pekka
  • 442,112
  • 142
  • 972
  • 1,088
  • @Nick I see! Then a `explode("-", $date);` and rearranging the array may be the best solution. – Pekka Nov 12 '10 at 10:32