6
echo date("m", strtotime("january"));

Returns 01 as expected

echo date("m", strtotime("february"));

But this returns 03

Anyone else encountered this problem?

PHP Version 5.1.6

StefWill
  • 353
  • 1
  • 3
  • 13

2 Answers2

24

Today is the 29th. There is no 29th in February this year and because you're not specifying a day in February, it's using "today". The strtotime function uses relative dates so the 29th of February is basically the 1st March this year.

To solve your problem:

echo date("m", strtotime("February 1"));
Francois Deschenes
  • 24,816
  • 4
  • 64
  • 61
  • 1
    "29th of February" is either 1st of March or 29th of February (rather than 2nd of March). "30th of February" would be either 1st or 2nd of March. Other than that, great answer. I think, StefWill should accept it. – binaryLV Jun 30 '11 at 06:30
  • @binaryLV - I guess I owe you an "excellent catch" too. :) I've updated my answer to reflect that. Thanks! – Francois Deschenes Jun 30 '11 at 06:31
  • I didn't realise that if no day is specified it used the current day. – StefWill Jun 30 '11 at 21:36
-1

As strtotime() only handles English date formats, you should maybe try using this function that I just made for you. With this you can handle month names in other languages too.

Don't know if this is essential to your application, but now you have it.

function getMonth($month, $leadingZero = true) {
    $month = strtolower(trim($month)); // Normalize

    $months = array('january' => '1',
                    'february' => '2',
                    'march' => '3',
                    'april' => '4',
                    'may' => '5',
                    'june' => '6',
                    'july' => '7',
                    'august' => '8',
                    'september' => '9',
                    'october' => '10',
                    'november' => '11',
                    'december' => '12',
                    'dezember' => '12', // German abrevation
                    'marts' => '3', // Danish abrevation for March 
                   );

    if(isset($months[$month])) {
        return $leadingZero ? substr('0' . $months[$month], -2) : $months[$month];
    } else {
        return false;
    }
}
Phliplip
  • 3,582
  • 2
  • 25
  • 42