简体   繁体   中英

Array of time periods with start and end date by range

I want to generate a table with values based on an array of time intervals that shows the start and end dates of each period. The periods can be in fixed intervals, example (P1D, P1M, etc).

I already have a function that generates the closed (past) periods, but I can not find a way to do it with future dates.

This is the function:

function interval($start, $interval, $break = 0)
{
    $result = array();

    $today = date("Y-m-d");
    if ($break == 0) {
        if ($today < $start) $break = 1;
    }

    $date = new DateTime($start);
    $interval = new DateInterval($interval);

    $f1 = date('Y-m-d', strtotime($start));

    $x = TRUE;
    if ($break == 0) {
        do {
            $date->add($interval);
            $f2 = $date->format('Y-m-d');
            $result[] = array(
                'begin' => $f1,
                'end'    => $f2
            );
            $x = ($f1 <= $today && $f2 >= $today) ? TRUE : FALSE;
            $f1 = date('Y-m-d', strtotime('+1 day', strtotime($f2)));

        } while (!$x);

    } else {

        for ($n = 1; $n <= $break; $n++) {
            $date->add($interval);
            $f2 = $date->format('Y-m-d');
            $result[] = array(
                'begin' => $f1,
                'end'    => $f2
            );
            $f1 = date('Y-m-d', strtotime('+1 day', strtotime($f2)));
        }
    }
    return $result;
}

For use simply call to function:

print_r(interval('2019-01-01', 'P1M'));

And result this:

(
    [0] => Array
        (
            [begin] => 2019-01-01
            [end] => 2019-02-01
        )

    [1] => Array
        (
            [begin] => 2019-02-02
            [end] => 2019-03-01
        )

)

I am looking to do the same but with future periods, for example, from 01-01-2019 to 01-01-2023 in 1-month intervals (P1M).

Update

The answer of @dWinder is the closest thing to the solution I looking for without having to travel in time =), and it works very well with monthly and annual intervals, but with shorter periods (P15D, P1W, P1D), the script presents problems that I can not identify.

I updated the function that you published so that it ran through the array correctly and stayed like this:

function interval($start, $interval, $maxPeriods = 100, $end = NULL) {
    $result = array();
    //$today = date("Y-m-d"); ** Variable not used, disable it **
    $date = new DateTime($start);
    $interval = new DateInterval($interval);

    $f1 = date('Y-m-d', strtotime($start));
    do {
        $date->add($interval);
        $f2 = $date->format('Y-m-d');
        $result[] = array( 'begin' => $f1, 'end' => $f2);
        if ($end <> NULL && $end == $f2) break; // if over the end date stop the loop
        $f1 = date('Y-m-d', strtotime('+1 day', strtotime($f2)));

    } while (++$maxPeriods); // I changed -- to ++ to avoid stopping the loop
    return $result;
}

The updated function is used in this way:

print_r(interval('2019-01-01', 'P1M', NULL, '2023-01-01'));

And the results:

(
    [0] => Array
        (
            [begin] => 2019-01-01
            [end] => 2019-02-01
        )

    [1] => Array
        (
            [begin] => 2019-02-02
            [end] => 2019-03-01
        )

    [2] => Array
        (
            [begin] => 2019-03-02
            [end] => 2019-04-01
        )

    [3] => Array
        (
            [begin] => 2019-04-02
            [end] => 2019-05-01
        )

    [4] => Array
        (
            [begin] => 2019-05-02
            [end] => 2019-06-01
        )

    [5] => Array
        (
            [begin] => 2019-06-02
            [end] => 2019-07-01
        )

    [6] => Array
        (
            [begin] => 2019-07-02
            [end] => 2019-08-01
        )

    [7] => Array
        (
            [begin] => 2019-08-02
            [end] => 2019-09-01
        )

    [8] => Array
        (
            [begin] => 2019-09-02
            [end] => 2019-10-01
        )

    [9] => Array
        (
            [begin] => 2019-10-02
            [end] => 2019-11-01
        )

    [10] => Array
        (
            [begin] => 2019-11-02
            [end] => 2019-12-01
        )

    [11] => Array
        (
            [begin] => 2019-12-02
            [end] => 2020-01-01
        )
)...

Better solution will be sending "end" argument and check it (in additional to the "maxPeriods"

Consider the follow (notice the comment in the code):

function interval($start, $interval, $maxPeriods = 100, $end = null) {
    $result = array();
    $today = date("Y-m-d");
    $date = new DateTime($start);
    $interval = new DateInterval($interval);

    $f1 = date('Y-m-d', strtotime($start));
    do {
            $date->add($interval);
            $f2 = $date->format('Y-m-d');
            $result[] = array( 'begin' => $f1, 'end' => $f2);
            if ($end && $end < $f2) break; // if over the end date stop the loop
            $f1 = date('Y-m-d', strtotime('+1 day', strtotime($f2)));

    } while (--$maxPeriods); //till you get max elements
    return $result;
}

Now call it with

print_r(interval('2019-01-01', 'P1M', 48));
//or
print_r(interval('2019-01-01', 'P1M', null, "2023-01-01"));

Notice that the first role encounter with break the loop (end date or max elements)

A time machine would help :) If the program correctly calculates past periods, then if it "thought" today was '2023-01-01' (give or take), it would correctly calculate the past periods. Can you add $end argument, default to today?

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