2013-06-04 205 views
0

我想在網站上自動顯示每個月的第一個和第三個星期一。如果當前日期在第一個星期一之後,則僅顯示第三個星期一。獲取每月的第一個和第三個星期一 - strtotime

我修改了在論壇上發現的代碼並做了一些更改,但由於有限的php知識,我無法驗證它是否會提供正確的結果。

$time = strtotime("first Monday of ".$monthname." ".$year); 
$time2 = strtotime("third Monday of ".$monthname." ".$year); 
{ 
    if ($time2 > $time) { 
     echo date('d-m-Y',$time2)." ".$monthname; 
    } 
    else { 
     echo date('d-m-Y',$time)." ".$monthname; 
    } 
} 

回答

0

我不完全相信你的意思,但這個應該怎麼做我想你想:

$time1 = strtotime("first Monday of {$monthname} {$year}"); 
$time2 = strtotime("third Monday of {$monthname} {$year}"); 
echo date('jS F Y', time() > $time1 ? $time2 : $time1); // e.g. 1st January 1970 

time() > $time1 ? $time2 : $time1是三元條件基本上是指

condition ? if_true : if_false 

展望順便說一下,我認爲你需要知道你可以把變量放在雙引號內,例如

$a = 'first'; 
echo "The $a day of the week"; // echoes 'The first day of the week 

但不是在單引號中,例如,

$a = 'first'; 
echo 'The $a day of the week'; // echoes 'The $a day of the week. 

我把周圍的變量,大括號出公約的,這樣我可以做

$a = 'first'; 
$b = 'variable'; 
echo "This is the {$a}_{$b}"; // Echoes 'This is the first_variable' 

沒有括號

echo "This is the $a_$b" // Undefined variable $a_ 

try { 
    // Do something 
} catch (Exception $ex) { 
    echo "There was an error and the message was {$ex->getMessage()}"; 
} 
相關問題