3

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.

hakre
  • 193,403
  • 52
  • 435
  • 836
Richard Knop
  • 81,041
  • 149
  • 392
  • 552

5 Answers5

7

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.

Gordon
  • 312,688
  • 75
  • 539
  • 559
  • Which is today, tomorrow, Friday, Monday, Tuesday :P – Richard Knop Jun 01 '11 at 14:29
  • @Richard errm, if that's not what you need, you might want to clarify the question. – Gordon Jun 01 '11 at 14:31
  • The week is from Monday to Friday. So for today I want 30.5, 31.5, 1.6, 2.6, 3.6. Your days span through 2 weeks. I think my question made that clear. Thanks anyways, I have figured it out :) – Richard Knop Jun 01 '11 at 14:39
  • @Robert given that none of the answers went back to the Monday, I'd say the question didnt make it *that* clear ;) See http://stackoverflow.com/questions/4150435/php-strtotime-last-monday-if-today-is-monday to get the first Monday of the week. – Gordon Jun 01 '11 at 14:43
  • Yeah, I guess I have been too vague. +1. – Richard Knop Jun 01 '11 at 14:47
  • Nice. But DatePeriod works only from >= PHP 5.3. I'm stuck with PHP 5.1.something right now (and I cannot update because of our ancient legacy applications). – Richard Knop Jun 02 '11 at 09:16
  • @Richard Why do you work with the DateTime API when half of the DateTime API doesnt work in 5.1? – Gordon Jun 02 '11 at 10:50
  • @Gordon Our senior PHP developer insists on using DateTime class and at the same time our server administrator does not want to upgrade to latest PHP version in forseeable future. – Richard Knop Jun 02 '11 at 15:30
  • But my solution works ok even though there are lots of loops. – Richard Knop Jun 02 '11 at 15:30
1

Try:

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

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

Sean
  • 696
  • 2
  • 9
  • 24
  • I assumed he would already know when Mondays are, but you're right, it is not the most exhaustive solution. – Sean Jun 01 '11 at 14:29
1

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.

Damien
  • 674
  • 5
  • 12
0

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);
}
tCot
  • 307
  • 2
  • 7
0

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 />';
}
Krishna Karki
  • 1,304
  • 4
  • 14
  • 31
Richard Knop
  • 81,041
  • 149
  • 392
  • 552