2013-06-04 124 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()}"; 
} 
相关问题