PHP查找每周特定的日子

时间:2019-04-21 22:03:11

标签: php

如何查询日期时间。

我的代码结果:

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

我要:

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

我的代码:

<?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 个答案:

答案 0 :(得分:0)

我不知道您要达到什么目的,但是此代码似乎有效:

$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 />';
}

结果:

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