Get all Work Days in a Week for a given date

So I have one date as a string:

2011/06/01

I need to get the 5 DateTime objects from it that correspond to the five weekdays (Monday to Friday) in that week, e.g. for the date above I need 2011-05-30 to 2011-06-03.

How to do that? I know I can do:

$dateTime = new DateTime('2011/06/01');

But I am kinda stuck there 🙂 I know, embarrassing.

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

Can use DatePeriod:

$firstMondayThisWeek= new DateTime('2011/06/01');
$firstMondayThisWeek->modify('tomorrow');
$firstMondayThisWeek->modify('last Monday');

$nextFiveWeekDays = new DatePeriod(
    $firstMondayThisWeek,
    DateInterval::createFromDateString('+1 weekdays'),
    4
);

print_r(iterator_to_array($nextFiveWeekDays));

Note that DatePeriod is an Iterator, so unless you are really fixed on having the dates in an array, you can just as well go with the DatePeriod as container.

The above will give something like (demo)

 Array
(
[0] => DateTime Object
    (
        [date] => 2011-05-30 00:00:00
        [timezone_type] => 3
        [timezone] => Europe/Berlin
    )

[1] => DateTime Object
    (
        [date] => 2011-05-31 00:00:00
        [timezone_type] => 3
        [timezone] => Europe/Berlin
    )

[2] => DateTime Object
    (
        [date] => 2011-06-01 00:00:00
        [timezone_type] => 3
        [timezone] => Europe/Berlin
    )

[3] => DateTime Object
    (
        [date] => 2011-06-02 00:00:00
        [timezone_type] => 3
        [timezone] => Europe/Berlin
    )

[4] => DateTime Object
    (
        [date] => 2011-06-03 00:00:00
        [timezone_type] => 3
        [timezone] => Europe/Berlin
    )
)

One pre-5.3 solution to do that would be

$firstMondayInWeek = strtotime('last Monday', strtotime('2011/06/01 +1 day'));
$nextFiveWeekDays = array();
for ($days = 1; $days <= 5; $days++) {
    $nextFiveWeekDays[] = new DateTime(
        date('Y-m-d', strtotime("+$days weekdays", $firstMondayInWeek))
    );
}

though I really dont see why you would want to use DateTime objects for this when you dont/cannot also use their API in your project. As you can see, this is all the old date functions with DateTime just being the container.

Solution 2

Try:

$dayAfter = $dateTime->modify('+1 day');

to get one day forward.
For more information check the php manual on this class here.

Solution 3

You can get the number of the day in the week with date(‘w’) 0 (for Sunday) through 6 (for Saturday). From this you can get the rest of the work days with strtotime(“+1 DAY”, [your timestamp]) and so on. When you’ve got the dates you can make the objects.

Solution 4

This seems to work:

$d = date('N', strtotime('2011/06/01'));
// here N means ISO-8601 numeric representation of the day of the week (added in PHP 5.1.0)
$week = array();
for($i = 1; $i < $d; ++$i){
    $dateTime = new DateTime('2011/06/01');
    for($j = 0; $j < $i; ++$j){
        $dateTime->modify('-1 day');
    }   
    $week[] = $dateTime;
}
$week[] = new DateTime('2011/06/01');
for($i = $d+1; $i <= 7; ++$i){
    $dateTime = new DateTime('2011/06/01');
    for($j = 0; $j < $i - $d; ++$j){
        $dateTime->modify('+1 day');
    }   
    $week[] = $dateTime;
}
sort($week);

foreach($week as $day){
    echo $day->format('Y-m-d H:i:s'), '<br />';
}

Solution 5

I’ve done this for 1 complete week.
I created one function which return the list of all days in a week.
I let you the code below

public function daysOfWeekXML($day)
{
    // Give number of day in the week
    $day_number = date('N', strtotime($day));

    $day_week_futur = [];
    $day_week_past = [];

    // Retrieve future days in the week
    for ($i = $day_number; $i <= 7; $i++) {
        $next_day = strtotime('+' . $i - $day_number . ' day', strtotime($day));
        array_push($day_week_futur, date('Y-m-d', $next_day));
    }

    // Retrieve days past in the week
    for ($day_number; $day_number > 1; $day_number--) {
        $previous_day = strtotime('-' . ($day_number - 1) . ' day', strtotime($day));
        array_push($day_week_past, date('Y-m-d', $previous_day));
    }
    // Concatenate all days in the week in array
    return array_merge($day_week_past, $day_week_futur);
}

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply