开发者

Bug with strtotime PHP

echo date("m", s开发者_Python百科trtotime("january"));

Returns 01 as expected

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

But this returns 03

Anyone else encountered this problem?

PHP Version 5.1.6


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"));


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;
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜