4

I encountered with this question today on StackOverflow but didn't get answer.

My question is

echo date('Y-m-d',strtotime('2012-september-09')); // output - 2012-09-01

echo date('Y-m-d',strtotime('09-september-2012')); // output - 2012-09-09

Codepad

I am confused that why the first format don't produce correct answer. Any Help?

Ashwini Agarwal
  • 4,828
  • 2
  • 42
  • 59

3 Answers3

6

From the manual:

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.

JvdBerg
  • 21,777
  • 8
  • 38
  • 55
4

This is not behavior of the date function but of the strtotime. strtotime can handle only dates in specific formats listed on the Date Formats manual page. 09-september-2012 is in the dd ([ \t.-])* m ([ \t.-])* y format listed there, but 2012-september-09 does not match any of the supported formats.

lanzz
  • 42,060
  • 10
  • 89
  • 98
1

I think in your code,

echo date('Y-m-d',strtotime('2012-september-09'));

strtotime('2012-september-09') is not a valid format ('2012-september-09') for strtotime() date function.

refer this link http://www.php.net/manual/en/datetime.formats.date.php

Ashwini Agarwal
  • 4,828
  • 2
  • 42
  • 59
Ravichandran Jothi
  • 3,028
  • 11
  • 52
  • 82