繁体   English   中英

strtotime PHP 的错误

[英]Bug with strtotime PHP

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

按预期返回 01

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

但这会返回 03

还有其他人遇到过这个问题吗?

PHP 版本 5.1.6

今天是29号。 今年 2 月没有 29 日,因为您没有指定 2 月的某一天,所以它使用“今天”。 strtotime function 使用相对日期,因此 2 月 29 日基本上是今年的 3 月 1 日。

要解决您的问题:

echo date("m", strtotime("February 1"));

由于strtotime()仅处理英文日期格式,您应该尝试使用我刚刚为您制作的 function。 有了这个,您也可以处理其他语言的月份名称。

不知道这对您的应用程序是否必不可少,但现在您拥有了。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM