time - Find next recurrence of weekly event if PHP "N" date value is given

114

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?

330

Answer

Solution:

You should use \DateTime and not 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
}

EDIT:

$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");

People are also looking for solutions to the problem: php - Sort array of days from a specific start date

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.