簡體   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