-3

I have a PHP array of days of the week:

$dow = array( 2, 3, 4, 6, 7 );

This array is generated dynamically. So it may be the values above, or it may be a slightly altered version.

I need to show the next week, with the days in that array highlighted, but it needs to be done starting with the current day.

So if today's day of the week is later, and I use the above array to get the next Tuesday, Wednesday, etc, then I end up missing the end of the current week.

Super short example:

$today = date();
foreach $dow as $id => $weekday:
    // display next $weekday after $today
endforeach;

So if today is Wednesday, then this will end up skipping Thurs, Sat, and Sun and start with Tuesday.

I can only figure out two possible ways to do this, and I'm not sure how to do either one. Either sort the array starting with the next day of the week and put the other ones at the end of the array, or get the starting position, loop through the rest of the array, then loop through the array again until I've hit that starting position.

How do I get the next week's worth of these days?

PBwebD
  • 778
  • 11
  • 33
  • Can you show some examples what you actually need? I don't know what you mean by current day + next weeks days from array. Two things that might help you are using `\DateTime()` instead of `date()` (so you can add a timestamp interval of 1 day) and PHP's sort function using a callback instead of a fixed algorithm. – Daniel W. Oct 31 '16 at 14:09
  • But I don't need just one day after another. Out of the next seven days, I need only the days that fall on the weekdays in the array. That's what I need. I need the next one of each of those days. The order of that array will need to change based on what the current day of the week is. – PBwebD Oct 31 '16 at 14:33
  • Possible duplicate of [how to create array of a week days name in php](http://stackoverflow.com/questions/2536748/how-to-create-array-of-a-week-days-name-in-php) – Kindle Q Feb 03 '17 at 12:07

1 Answers1

1
$today = date('N');

foreach ($dow as $key => $value) {
    if ($value >= $today) {
        break;
    }
}

$dow = array_merge(array_slice($dow, $key), array_slice($dow, 0, $key));

If today is 4, the array is now:

array(4, 6, 7, 2, 3)

You just need to loop over it in order now.

deceze
  • 510,633
  • 85
  • 743
  • 889