0

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?

cukabeka
  • 530
  • 2
  • 9
  • 22
  • 3
    Wow, that is really ugly code. Why not break it up into multiple conditions if it works properly? – Nico Haase Oct 02 '18 at 11:26
  • I broke it up and gave up before because I didn't get the basic idea from the solution in the link provided. I am willing to have a complete rewrite. ;) – cukabeka Oct 02 '18 at 11:28
  • Could you explain a bit how $row['tuesday'] works, is there, for example, a $row['sunday'] too, and for every other day of the week? – T K Oct 02 '18 at 11:35
  • Uh, sorry, my mistake. It should be just "weekday". I edit the code above – cukabeka Oct 03 '18 at 20:41

1 Answers1

0

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");
Code Spirit
  • 3,992
  • 4
  • 23
  • 34
  • Hi, thanks for your help! The point is that $eventDate is not specified. The only thing available is a numeric value for the weekday. I only know that the event takes place on tuesdays at 12:00 - no specific date like Y-m-d – cukabeka Oct 03 '18 at 20:45