2

I am trying to convert string month name to month number,

but why do I get '03' in the result for 'Feb' in,

strtolower(date('m', strtotime('Feb')));

I tested with other month names and they seem to be fine,

strtolower(date('m', strtotime('Jan'))); // 01
strtolower(date('m', strtotime('Mar'))); // 03

What have done wrong?

Run
  • 54,938
  • 169
  • 450
  • 748
  • 7
    because today is the 30th. Try adding '1st' to every string – Rene Pot Jan 30 '12 at 12:35
  • 3
    Indeed: try `strtolower(date('m', strtotime('1 Feb')))` – lonesomeday Jan 30 '12 at 12:36
  • 1
    You have not specified the day nor the year. And in any case you run into a problem with a specific PHP function, please first read the manual entry about it: http://php.net/strtotime – hakre Jan 30 '12 at 12:41

2 Answers2

2

Because today is Jan 30th. You are not supplying a day number so php assumes today's, ending up with Feb 30th. Which it then realises is not valid, so it goes on to Mar 2nd, poor confused thing.

Rob Agar
  • 12,337
  • 5
  • 48
  • 63
2

From the php.net manual:

The function expects to be given a string containing an English date format and will try to parse that format into a Unix timestamp (the number of seconds since January 1 1970 00:00:00 UTC), relative to the timestamp given in now, or the current time if now is not supplied.

This results in a mixture of todays date (January 30th) and "Feb" => February 30th - but this is not a valid date, so PHP returns the month number for March.

Trying something like

strtotime('01 Feb')

should solve the problem.

schnaader
  • 49,103
  • 10
  • 104
  • 136