PHP date()和strtotime()在31日返回錯誤的月份

[英]PHP date() and strtotime() return wrong months on 31st


I'm using date()and strtotime() functions to display the next 3 months in a dropdown list.

我正在使用date()和strtotime()函數在下拉列表中顯示接下來的3個月。

PHP Code:

   echo date("m/Y",strtotime("+0 months")); 
   echo date("m/Y",strtotime("+1 months"));
   echo date("m/Y",strtotime("+2 months")); 

However, if the script is running when the server date is on 30th or 31st, the next month, which is Feburary, will be displayed as March instead. i.e. the script above is supposed to return

但是,如果腳本在服務器日期為30或31時運行,則下個月(即2月)將顯示為3月。即上面的腳本應該返回

01/2012
02/2012
03/2012

But, instead of that, it actually displays

但是,它實際上顯示了而不是那樣

01/2012
03/2012
03/2012

that is because Feburary doesn't have 30th or 31st, so the script translates "31/02" into "01/03".

那是因為2月沒有30或31,所以腳本將“31/02”翻譯成“01/03”。

I have read the strtotime() page on php.net, this problem has been raised, but there has not been any useful solutions. So can anyone please help me to find a simple way to solve this problem? Thanks in advance!

我已經閱讀了php.net上的strtotime()頁面,這個問題已經提出,但是沒有任何有用的解決方案。那么有人可以幫我找一個解決這個問題的簡單方法嗎?提前致謝!

4 个解决方案

#1


27  

As mentioned within the documentation, you should pass the date for the first day of the current month as the second parameter to the strtotime() function:

如文檔中所述,您應該將當月第一天的日期作為第二個參數傳遞給strtotime()函數:

$base = strtotime(date('Y-m',time()) . '-01 00:00:01');
echo date('m/Y',strtotime('+0 month', $base));
echo date('m/Y',strtotime('+1 month', $base));
echo date('m/Y',strtotime('+2 month', $base));

See that it works: http://ideone.com/eXis9

看它是否有效:http://ideone.com/eXis9

01/2012

02/2012

03/2012

#2


10  

Try using "first day of" in your strtotime, like this:

嘗試在你的strtotime中使用“first day of”,如下所示:

strtotime("first day of +1 month");

This will fix dates ( in the event today was Jan 30th ) such as 02-30 (Yields march 2nd) by converting it to 02-01 (Feb 1st) which then gives you the correct month. It's a little cleaner than other methods, and easier to remember.

這將修復日期(在今天是1月30日的事件中),例如02-30(Yields march 2nd),將其轉換為02-01(2月1日),然后給出正確的月份。它比其他方法更清潔,更容易記住。

#3


3  

echo date('m/Y', strtotime(date('Y-m') . '-01 +2 months'));

Just hard-code it to be the first of the month.

只需將其硬編碼為本月的第一天。

#4


0  

Don't use strtotime() for getting offset date by month(s). It works properly only in PHP 5.3+. The best way to solve such problem is using mktime(). Below is a sample code:

不要使用strtotime()來逐月獲得偏移日期。它僅適用於PHP 5.3+。解決此類問題的最佳方法是使用mktime()。以下是示例代碼:

function getOffsetByMonths($nMonths, $nNow = 0) {
    if ($nNow)
        return mktime(0, 0, 0, date('n', $nNow)+ $nMonths, 1, date('Y', $nNow));
    else
        return mktime(0, 0, 0, date('n')+ $nMonths);
}
$nNow = mktime(0, 0, 0, 1, 31, 2013);
echo "Now: ". date("Y-m-d", $nNow).
"<br>(Now - 1 month): ". date("Y-m", getOffsetByMonths(-1, $nNow)). "-xx".
"<br>(Now - 2 month): ". date("Y-m", getOffsetByMonths(-2, $nNow)). "-xx".
"<br>(Now - 3 month): ". date("Y-m", getOffsetByMonths(-3, $nNow)). "-xx";

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2012/01/30/7211319436a58528662e19c131412c2a.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com