我想在网站上自动显示每个月的第一个和第三个星期一。如果当前日期在第一个星期一之后,则只需显示第三个星期一。
我修改了在论坛上发现的代码并进行了一些更改,但由于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 :(得分: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
是三元条件,基本上是指</ p>
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()}";
}