How can I query with datetime.

My code result:

08-06-2019, Saturday
15-06-2019, Saturday
22-06-2019, Saturday
09-06-2019, Sunday
16-06-2019, Sunday
23-06-2019, Sunday

I want to:

08-06-2019, Saturday
09-06-2019, Sunday
15-06-2019, Saturday
16-06-2019, Sunday
22-06-2019, Saturday
23-06-2019, Sunday

My code:

<?php
$week       = 1;
$days       = array(7, 8);
$start_date = new DateTime('2019-06-01');
$end_date   = new DateTime('2019-06-16');

$interval   = new DateInterval("P{$week}W");
$period     = new DatePeriod($start_date, $interval, $end_date);

foreach($days as $day){
    foreach($period as $date){
        $date->modify("+{$day} day");
        echo $date->format('d-m-Y') . ', ' . $date->format('l') . '<br />';
    }
}
?>

1 Answers

0
Alex Karshin On

I have no idea what you're trying to achieve, but this code seems to be working:

$week       = 1;
$days       = array(7, 8);
$start_date = new DateTime('2019-06-01');
$end_date   = new DateTime('2019-06-16');

$interval   = new DateInterval("P{$week}W");
$period     = new DatePeriod($start_date, $interval, $end_date);

foreach($period as $date) {
    $date->modify("+7 days");
    echo $date->format('d-m-Y, l') . '<br />';

    $sunday = clone $date;
    $sunday->modify("+1 day");
    echo $sunday->format('d-m-Y, l') . '<br />';
}

Result:

08-06-2019, Saturday
09-06-2019, Sunday
15-06-2019, Saturday
16-06-2019, Sunday
22-06-2019, Saturday
23-06-2019, Sunday