6

我在我的一个脚本中运行了以下代码,它运行良好,但感觉相当笨重且冗长。我觉得可能有更短的方法来实现相同的结果。我的意思是不使用简写 if 语句。

我需要确定今天是否是星期四,如果不是,则使用上一个星期四作为日期。任何想法/想法都会很棒。

<?php

if (new Carbon('this thursday') > new Carbon()) {
    $date = Carbon('this thursday');
} else {
    $date = Carbon('last thursday');
}

?>
4

1 回答 1

11

根据 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');
?>
于 2015-07-02T16:31:00.087 回答