我在我的一个脚本中运行了以下代码并且运行良好,但感觉相当笨重而且很长。我觉得可能有更短的方法来实现相同的结果。我的意思是不使用速记if语句。
我需要知道今天是星期四,如果不是以前的星期四作为约会。任何想法/想法都会很棒。
<?php
if (new Carbon('this thursday') > new Carbon()) {
$date = Carbon('this thursday');
} else {
$date = Carbon('last thursday');
}
?>
答案 0 :(得分:7)
根据 http://carbon.nesbot.com/docs/#api-modifiers:
$ date将保留显示的日期。
<?php
$today = new Carbon();
if($today->dayOfWeek == Carbon::THURSDAY)
$date = $today;
else
$date = new Carbon('last thursday');
?>