time - Find next recurrence of weekly event if PHP "N" date value is given
I have a weekday value available in the PHP date("N",$stamp) format + a time with an starting hour of an event. I want to show the date of next week if the event has started already and the date of this week if it is in the future. The time horizon is 7 days, so if "now" has passed, the expected recurrence is in 7 days.
Example
now() is tuesday, 2/10/2018, 13:00
$row['weekday'] = 2 (for tuesday)
$row['time'] = 13:01
$next should be 9/10/2018
vs.
now() is tuesday, 2/10/2018, 13:00
$row['weekday'] = 2 (for tuesday)
$row['time'] = 12:00
$next should be 2/10/2018
Here's the PHP documentation to the "N" time format:
N ISO-8601 numeric representation of the day of the week (added in PHP 5.1.0) 1 (for Monday) through 7 (for Sunday)
I investigated quite a bit and could not find any solution to this. The only thing I found was this which I based the following (ugly, non-working) code on.
$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")))
)
)
Any ideas how to tackle this?
Answer
Solution:
You should use \DateTime and not date():
EDIT: