简体   繁体   中英

PHP Weekly date range with specific start and end day

Hi I already have a date range result but how can I make the date range start at every Thursday and ends at Wednesday. Thanks for your help :)

$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
    )

So today is thursday so it is correct. But if today is friday then the result is wrong. If today is friday then I want the correct result above. The wrong result is:

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

This is painful to see using date() and strtotime() . Use DateTime() and its associated classes to make this easier to understand.

In my example below I use relative date/time formats which allows me to specify Thursday as an end date. I then go back six weeks to make an start date and an interval of one week. I then loop through them and put them in an array. I reverse the array and print it out.

$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

Obviously you can change the formatting to suit your needs.

you can use bellow code, same your way

<?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);

This is my revised correct answer based from my previous incorrect code:

<?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";
}

I changed my laptop date and set it to tomorrow (Friday) and the results are now correct using the code above:

Result:

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
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM