2012-01-20 33 views
1

問題:我正在使用strtotime在將來提前364天,但我遇到了閏年的麻煩。php:提前到下一年減去1天

舉例:今天是二○一二年一月二十〇日 - 我需要PHP來計算我1月19日的時間戳,2013年

如果我只需添加

的strtotime( 「+364天」);

我得到正確的2013年1月18日 - 但代碼我寫我不需要考慮閏年,因此我希望能獲得1月19日,2013年

任何快速和骯髒的方式來做這個?

+1

'strtotime(「+ 1 year」) - 86400'或'strtotime(「+ 1 year -1 day」)'? – TimWolla

+0

當然這對我來說很清楚,我想知道一個快速的方法來確定我什麼時候需要減去1天(並且從而驗證是否有2月29日的範圍) – Sebastiano

+0

'$ leapyear = $ year%4 == 0 && ($ year%100!= 0 || $ year%400 == 0)' – TimWolla

回答

3

strtotime()將閏年考慮在內。

strtotime('+1 year -1 day');

注意替換strtotime("+364 days");:不是每個人都想要讀了一堆的意見得到答案

0

您也可以嘗試檢查,如果今年是閏年與否,然後添加+/- 1天

<?php 

function is_leap() 
{ 
    $year = date('Y'); 
    $check = ((($year % 4) == 0) && ((($year % 100) != 0) || (($year %400) == 0))); 
    return $check; 
} 

$year = (is_leap()) : strtotime("+365 days") ? strtotime("+364 days") 

?>