How to call next or previous element?

<?php

function morgen($vandaag) {
  $dagen = array ('ma','di', 'wo','do', 'vr', 'za', 'zo');
  return $dagen[$vandaag];
}
echo morgen(2);
echo "<br>";

I am new on Php. I called (wo) and I wanna call the next one (vr) how can I do that?

The next one after Wednesday is Thursday…

anyway i guess you should increase the $vandaag value

$vandaag++;

But what if your on the last day? Day number 6 or sunday? Then tomorrow will be day 0 or monday. So you must add an overflow protection.

    while($vandaag > 6) {
        $vandaag -= 7;
    }

The whole function should then look like this

    function morgen($vandaag) {
      $dagen = array ('ma','di', 'wo','do', 'vr', 'za', 'zo');

      $vandaag++;

      while($vandaag > 6) {
           $vandaag -= 7;
      }

      return $dagen[$vandaag];
    }

[edit]
date(´w´) gives a numeric representation of the day of the week 0 (for Sunday) through 6 (for Saturday). To use that you should start wit Sunday in your array and ending with Saturday.

Now you are using the ISO-8601 numeric representation of the day of the week. But that one starts with one instead of zero. In that case you could forget the $vandaag++; rule and change the while condition to while($vandaag > 7)

1 Like

So, what is the actual problem rather than your attempted solution?

If you just want to find out what tomorrow is,

function tomorrow() {
    $date = new DateTime();
    return $date->modify('+1 day');
}

echo tomorrow()->format('Y-m-d');
// outputs 2020-05-27
1 Like
Sponsor our Newsletter | Privacy Policy | Terms of Service