1. 程式人生 > >php strtotime 函式 獲取上月日期的異常表現

php strtotime 函式 獲取上月日期的異常表現

       最近寫一個指令碼,獲取當前日期上個月的年和月,噹噹前日期是2013年12月31日的時候,採用date('Y-m-d', strtotime('-1 month'))獲取的上個月的日期居然是2013-12-01。頓時有點費解。後來上網一查,發現很多人也發現了這個問題。

     一般解決辦法是獲取當前月,然後判斷上個月是不是跨年了來解決。

   if( 1 == date('n') )
{
    $strLastMonthYear = date('Y') - 1;
    $strLastMonthMonth = 12;
}
else
{
    $strLastMonthYear = date('Y');
    $strLastMonthMonth = date('n') - 1;
}