PHP 的 DateTime 修改不会返回正确的日期 [关闭]

问题描述 投票:0回答:1

我想用

DateTime
modify
获得一周的星期日,但它不会返回预期的日期。

$date = new \DateTime('2050-03-01');
$date->add(new \DateInterval("P1W"));
$date->modify("sunday this week");

$date
返回为
2050-03-13
而不是
2050-03-06
,正如您在日历上看到的那样: March 2050 calendar

如果有人想知道,我正在使用

America/Sao_Paulo
时区,我也不能只使用
sunday last week
(有效),因为我在内部将其用于使用一周中所有日子的算法。

另一件事是,当

modify
monday this week
时,它实际上返回正确的日期
2050-03-07
。我搜索了一下,PHP DateTime 认为星期日是一周的第一天,所以我真的不知道这里会发生什么。

php datetime yii2
1个回答
0
投票

我的建议

strtotime()

strtotime("next Thursday")

看到这个链接

strtotime
返回一个时间戳,您可以使用
date
函数将时间戳转换为日期

© www.soinside.com 2019 - 2024. All rights reserved.