每个月的第一个和第三个星期一——时间



我想在网站上自动显示每个月的第一个和第三个星期一。如果当前日期在第一个星期一之后,则只显示第三个星期一。

我已经修改了在论坛上发现的代码,并做了一些更改,但由于有限的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;
    }
}  

我不完全确定你的意思,但这应该做我认为你想要的:

$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()}";
}

最新更新