我有一个工作日值,以PHP (“N”,$stamp)格式+一个事件开始时间为单位。如果事件已经开始,我想显示下个星期的日期,如果是在将来,我要显示这个星期的日期。时间是7天,所以如果“现在”已经过去,那么预期的复发将在7天内发生。
示例
now() is tuesday, 2/10/2018, 13:00
$row['weekday'] = 2 (for tuesday)
$row['time'] = 13:01
$next should be 9/10/2018与
now() is tuesday, 2/10/2018, 13:00
$row['weekday'] = 2 (for tuesday)
$row['time'] = 12:00
$next should be 2/10/2018以下是"N“时间格式的PHP文档:
N ISO-8601一周中的一天的数字表示(在PHP5.1.0中添加)1(星期一)到7(星期日)
我调查了很多次,却找不到任何解决办法。我发现的唯一一件事是这,它是基于以下(丑陋的、不工作的)代码的。
$next = (intval(date("N", strtotime("now"))) !== intval($row['weekday']))
?
(
date("d.m.Y", strtotime(
date("Y-m-d", strtotime("now")-strtotime("+". (date("w", strtotime("now")) +0) ." day"))
)+strtotime("+".$row['weekday']." day"))
)
:
(
(
(strtotime("now"))
<
(( strtotime(
date("Y-m-d", strtotime("now")-strtotime("+". (date("w", strtotime("now")) +0) ." day"))
)+strtotime("+".$row['weekday']." day")+strtotime($row['time'])))
)
?
(date("d.m.Y", strtotime("now")))
:
(date("d.m.Y", strtotime("now +1 week")))
)
)有什么办法解决这个问题吗?
发布于 2018-10-02 11:54:02
您应该使用\DateTime而不是date():
$eventDate = \DateTime::createFromFormat("Y-m-d H:i:s", $date);
$today = \DateTime::createFromFormat("N", $weekDay);
$next = clone $today;
if ($today > $eventDate) {
$next->modify("+1 week"); //$next->add(new \DateInterval("P1W"))
//Eventually also set time
}编辑:
$now = new \DateTime();
$date = \DateTime::createFromFormat("D", "Tue"); //W and N are not working
$dayPeriod = new \DateInterval("P1D");
//Check if in last week
if ($date->format("W") < $now->format("W")) {
$date->add($dayPeriod);
}
//Check if in current week
if ($date->format("W") === $now->format("W")) {
$date->add($dayPeriod);
}
echo $date->format("Y-m-d");https://stackoverflow.com/questions/52607395
复制相似问题