Search code examples
phpdatetimedatephp-5.3strtotime

Month name to month number - 03 for Feb?


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?


Solution

  • 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.