2014-12-24 63 views
0

我想在每週前後的一週前和後一週內回覆「Hello World」,並在每年12月25日前後包括這些內容標誌更換者)。不幸的是,我在+/- 1周的部分卡住邏輯,我不知道該怎麼做,到目前爲止,如果日期是12月25日,我已經工作了。在設定日期前一週和一週後顯示消息

<?php 

$xmas = date('Y').'-12-25'; 

$today = date('Y-m-d', strtotime(date('Y-m-d'))); 

if ($today == date('Y-m-d', strtotime($xmas))) { 
    echo 'hello world!'; 
} 

?> 

所以基本上25是具有固定間隔一週中心點二者之後和之前,在這應該顯示該消息。

+0

爲u需要從日期和14-20 28-3 2015年1月 –

+0

實例顯示消息:'$日期=日期( 「YMD」); //當前date'然後'$日期=的strtotime(日期(「Ymd」,strtotime($ date))。「+1 week」);''和'$ date = strtotime(date(「Ymd」,strtotime($ date))。你+1周和-1周。 –

+0

@ Fred-ii-是的,但有問題與形成一個邏輯表達式 – Alex

回答

1
<?php 

$date = date('Y').'-12-25'; 

$today = strtotime(date('Y-m-d')); 

$dateMin = strtotime($date . " -1 week"); 
$dateMax = strtotime($date . " +1 week"); 

echo $dateMin . '<br>' . $dateMax . '<br>' . $today; 

if (($today >= $dateMin) && ($today <= $dateMax)) { 

    echo 'date is in range'; 

} 

?> 
+0

我認爲這是你的解決方案,是嗎? –

相關問題