1

I'm trying to create an array of the next 5 working week days (Monday - Friday, excluding today). I know the working week varies around the world but this is not important for what I am trying to do.

So, for example, if today is a Wednesday, I want the dates for Thursday and Friday of the current week and Monday, Tuesday and Wednesday of the following week.

I thought this would work:

$dates = array();

for ($i = 1; $ < 6; $i ++)
{
    $dates[] = date('Y-m-d', strtotime('+ '.$i.' weekday'));
}

But for today, it is giving me:

  • Monday 1st
  • Tuesday 2nd
  • Wednesday 3rd
  • Thursday 4th
  • Sunday 7th!

Any advice appreciated.

Thanks

Dan
  • 6,265
  • 8
  • 40
  • 56
  • friday and saturday is missing, are you in the middle east? – DevZer0 Jun 28 '13 at 10:00
  • Pretty weird... `strtotime('yesterday+ '.$i.' weekday')` seems to fix it for me (just remove the first element afterwards). Must be a bug in PHP then... – Nils Werner Jun 28 '13 at 10:27
  • There appear to have been quite a few bug report about using this function in PHP `#63521` seems to be that latest. An answer here may be of use `http://stackoverflow.com/a/13131740/2310830` but basically it looks like you will have to create your own code to do this correctly. – RiggsFolly Jun 28 '13 at 10:50

4 Answers4

8

Try this

$dates = array();
$date = new DateTime();

while (count($dates)<5)
{
    $date->add(new DateInterval('P1D'));
    if ($date->format('N')<6)
        $dates[]=$date->format('Y-m-d');
}
Alessandro
  • 1,443
  • 9
  • 18
1
function getWeekdayDatesFrom($format, $start_date_epoch, $end_date_epoch, $range) {

    $dates_arr = array();

    if( ! $range) {
        $range = round(abs($start_date_epoch-$end_date_epoch)/86400) + 1;
    } else {
        $range = $range + 1; //end date inclusive
    }

    $current_date_epoch = $start_date_epoch;

    for($i = 1; $i <= $range; $i+1) {

        $d = date('N',  $current_date_epoch);

        if($d <= 5) { // not sat or sun
            $dates_arr[] = "'".date($format, $current_date_epoch)."'";
        }

        $next_day_epoch = strtotime('+'.$i.'day', $start_date_epoch);
        $i++;
        $current_date_epoch = $next_day_epoch;

    }

    return $dates_arr;
} 
womplefrog
  • 769
  • 1
  • 6
  • 18
  • This should answer the question, and then some. With this function you can declare the return format (i.e. Y-m-d), declare a start date and end date or a start date and range. I currently have it set to include the end date and it only returns weekdays by default (which can be easily undone). I have the dates returned in quotes as well to be more easily used for query clauses. – womplefrog Nov 18 '13 at 19:02
0

This should work:

$dates = array();
$i = 0;

while(true) {
    $i++;

    $time = strtotime("+$i days");
    $dayOfWeek = date('w', $time);

    /* 'w' - day of week, numeric, i.e. "0" (Sunday) to "6" (Saturday) */
    if( ($dayOfWeek == 0) or ($dayOfWeek == 6) ) {
        continue;
    }

    $dates[] = date('Y-m-d', $time);

    if( count($dates) >= 5 ) {
        break;
    }
}
tobia.zanarella
  • 1,246
  • 1
  • 17
  • 25
0

You can do like this:

$now = time();
$day = 60*60*24;
$days = array();
for ($i=1; $i<=7; $i++){
    $ts = $now + $day*$i;
    if (date("N", $ts) < 6){
        $days[] = date("l jS", $ts);
    }
}

The output for the above code is:

Array(
    [0] => Monday 1st
    [1] => Tuesday 2nd
    [2] => Wednesday 3rd
    [3] => Thursday 4th
    [4] => Friday 5th
)
Expedito
  • 7,771
  • 5
  • 30
  • 43