本周周四相对日期PHP



我可以通过以下方式获得本周星期四

$startdate = (new DateTime())->setTime(0,0,0);
$startdate->setISODate($startdate->format("Y"), $startdate->format("W"), 4);

但是,如果我尝试一个相对日期,它会在周四之前或周四的本周得到,并在周四之后得到下周四。

$startdate = (new DateTime('thursday'))->setTime(0,0,0);

我也尝试了"这个星期四"的相对日期,但它没有返回我会显示的内容,它的行为就像"星期四"。我可以使用 setISODate,但我只是好奇我是否可以使用相对日期,因为它会更容易。

http://php.net/manual/en/datetime.formats.relative.php

试试这个:

$startdate1 = (new DateTime('thursday last week'))->setTime(0,0,0); //2017-01-19
$startdate2 = (new DateTime('thursday this week'))->setTime(0,0,0); //2017-01-26
$startdate3 = (new DateTime('thursday next week'))->setTime(0,0,0); //2017-02-02

最新更新