PHP每周日期范围,具体的开始和结束日期

时间:2017-03-16 01:50:40

标签: php date strtotime date-range

您好我已经有了日期范围结果但是如何让日期范围从每个星期四开始到星期三结束。谢谢你的帮助:)

Design_btn02

所以今天是星期四,所以这是正确的。但如果今天是星期五,那么结果是错误的。如果今天是星期五,那么我想要上面的正确结果。错误的结果是:

$week_range = '';
for ($i = 0; $i <= 4; $i++) {
    $k = $i - 1;

    $ymd_week_range = date('Y-m-d', strtotime("-$i week")) . ',' . date('Y-m-d', strtotime("-$k week -1 day"));

    $day_from = date('j', strtotime("-$i week"));
    $day_to = date('j', strtotime("-$k week -1 day"));
    $month_to = date('M', strtotime("-$i week"));
    $month_from = date('M', strtotime("-$k week -1 day"));
    $year_to = date('Y', strtotime("-$i week"));
    $year_from = date('Y', strtotime("-$k week -1 day"));

    $weeks[$ymd_week_range] = "$month_from $day_from-$day_to, $year_to";   
}



Array
    (
        [2017-03-16,2017-03-22] => Mar 16-22, 2017
        [2017-03-09,2017-03-15] => Mar 9-15, 2017
        [2017-03-02,2017-03-08] => Mar 2-8, 2017
        [2017-02-23,2017-03-01] => Mar 23-1, 2017
        [2017-02-16,2017-02-22] => Feb 16-22, 2017
    )

3 个答案:

答案 0 :(得分:2)

使用date()strtotime()会很痛苦。使用DateTime() and its associated classes可以更容易理解。

在下面的示例中,我使用相对日期/时间格式,这允许我将星期四指定为结束日期。然后我回去六个星期来制定开始日期和一周的间隔。然后我循环遍历它们并将它们放在一个数组中。我将数组反转并打印出来。

$dates    = [];
$end      = new DateTimeImmutable('Thursday');
$start    = $end->modify('-6 weeks');
$interval = new DateInterval('P1W');
$period   = new DatePeriod($start, $interval, $end);
foreach ($period as $date) {
    $wednesday = $date->modify('Wednesday');
    $dates[] = sprintf('%s - %s%s', $date->format('D j M'), $wednesday->format('D j M'), PHP_EOL);
}
$dates = array_reverse($dates);
foreach ($dates  as $week) {
    echo $week;
}

Demo

Thu 9 Mar - Wed 15 Mar
Thu 2 Mar - Wed 8 Mar
Thu 23 Feb - Wed 1 Mar
Thu 16 Feb - Wed 22 Feb
Thu 9 Feb - Wed 15 Feb
Thu 2 Feb - Wed 8 Feb

显然,您可以更改格式以满足您的需求。

答案 1 :(得分:1)

你可以使用波纹管代码,同样的方式

<?php
$weeks = array();
$dayOfWeek = date('w');
$thurday = 4;
$diff = $thurday - $dayOfWeek;

for ($i = 0; $i <= 4; $i++) {
    $k = $i - 1;

    $ymd_week_range = date('Y-m-d', strtotime("-$i week")) . ',' . date('Y-m-d', strtotime("-$k week -1 day"));

    $day_from = date('j', strtotime("-$i week $diff day"));
    $day_to = date('j', strtotime("-$k week ".($diff - 1)." day"));
    $month_to = date('M', strtotime("-$i week $diff day"));
    $month_from = date('M', strtotime("-$k week ".($diff - 1)." day"));
    $year_to = date('Y', strtotime("-$i week $diff day"));
    $year_from = date('Y', strtotime("-$k week ".($diff - 1)." day"));

    $weeks[$ymd_week_range] = "$month_from $day_from-$day_to, $year_to";   
}

print_r($weeks);

答案 2 :(得分:0)

根据我之前的错误代码,这是我修正的正确答案:

<?php
for ($i = 0; $i <= 4; $i++) {
    $k = $i - 1;

    $from_text = strtotime("this week thursday -$i week");
    $to_text = strtotime("-$k week -1 day");

    $ymd_week_range = date('Y-m-d', $from_text) . ',' . date('Y-m-d', $to_text);

    $day_from = date('j', $from_text);
    $day_to = date('j', $to_text);

    $month_from = date('M', $from_text);
    $month_to = date('M', $to_text);

    $year_from = date('Y', $from_text);
    $year_to = date('Y', $to_text);

    $weeks[$ymd_week_range] = "$month_from $day_from - $month_to $day_to, $year_to";
}

我更改了我的笔记本电脑日期并将其设置为明天(星期五),现在结果使用上面的代码正确:

结果:

Array
(
    [2017-03-16,2017-03-23] => Mar 16 - Mar 23, 2017
    [2017-03-09,2017-03-16] => Mar 9 - Mar 16, 2017
    [2017-03-02,2017-03-09] => Mar 2 - Mar 9, 2017
    [2017-02-23,2017-03-02] => Feb 23 - Mar 2, 2017
    [2017-02-16,2017-02-23] => Feb 16 - Feb 23, 2017
)