2012-10-18 98 views
1

我使用代碼:mktime函數不能正常工作

$old_month = strtolower(date("m", mktime(date('H'), date('i'), date('s'), (date('m')-12),date('d'), date('Y')))); 
$old_year = strtolower(date("Y", mktime(date('H'), date('i'), date('s'), (date('m')-12), date('d'), date('Y')))); 


if ($profile_stats['month'] < $old_month && $profile_stats['year'] == $old_year 
|| $profile_stats['month'] == date('m') && $profile_stats['year'] == date('Y')) 

的如果loop應該採取的最後month..i.e的價值。它現在十月..如果應該搜索sept ie。 9

我該如何做到這一點。

請提出任何解決方案。

+0

可能重複http://stackoverflow.com/questions/12960824/php-script-getting -month-error) – Ray

回答

2

使用此

$old_month = strtolower(date("m", strtotime("lastmonth")); 


if ($profile_stats['month'] < $old_month && $profile_stats['year'] == $old_year 
|| $profile_stats['month'] == date('m') && $profile_stats['year'] == date('Y')) 

應該

if (strtotime($profile_stats['month']) < strtotime($old_month) && strtotime($profile_stats['year']) == strtotime($old_year) 
|| $profile_stats['month'] == date('m') && $profile_stats['year'] == date('Y')) 
+0

@ user1757184是不是它給出了正確的答案? – StaticVariable

+0

請立即看到問題。 –

+0

@ user1757184看到編輯的答案 – StaticVariable

0

你似乎是從月數減去12。那會在去年做到。

嘗試使用英語 - strtotime是在處理那些出乎意料的好:

$last_month = strtotime("-1 month"); 
list($old_month,$old_year) = explode("-",date("m-Y",$last_month)); 
[PHP腳本獲得一個月的錯誤(的
+0

不建議strtotime。使用DateTime和1個月的時間間隔。 –

+0

請現在看這個問題。 –