1

我想知道如果满足某些特定条件,是否可以将给定的日期范围拆分为 2 个单独的日期范围?

我正在建立一个假期预订系统,如果假期被预订超过 2 年,我需要拆分日期。例子:

start date: 22-12-2013
end date: 04-01-2014

对于上面的日期,我想将它们分成 2 个日期范围,所以我最终会得到这样的结果:

start date: 22-12-2013
end date: 31-12-2013

进而:

start date: 01-01-2014
end date: 04-01-2014

这可能吗?如果是这样,有人可以指出我需要在谷歌中搜索的正确方向吗?

4

5 回答 5

4

如果你想分割更多的月份,你可以使用这个功能:

function getMonthRanges($start, $end) {
    $timeStart = strtotime($start);
    $timeEnd   = strtotime($end);
    $out       = [];

    $milestones[] = $timeStart;
    $timeEndMonth = strtotime('first day of next month midnight', $timeStart);
    while ($timeEndMonth < $timeEnd) {
        $milestones[] = $timeEndMonth;
        $timeEndMonth = strtotime('+1 month', $timeEndMonth);
    }
    $milestones[] = $timeEnd;

    $count = count($milestones);
    for ($i = 1; $i < $count; $i++) {
        $out[] = [
            'start' => $milestones[$i - 1], // Here you can apply your formatting (like "date('Y-m-d H:i:s', $milestones[$i-1])") if you don't won't want just timestamp
            'end'   => $milestones[$i] - 1
        ];
    }

    return $out;
}

使用:

$result = getMonthRanges('15-11-2014', '19-11-2014');

结果:

Array
(
    [0] => Array
        (
            [start] => 1418594400
            [end] => 1420063199
        )

    [1] => Array
        (
            [start] => 1420063200
            [end] => 1422741599
        )

    [2] => Array
        (
            [start] => 1422741600
            [end] => 1425160799
        )

    [3] => Array
        (
            [start] => 1425160800
            [end] => 1426715999
        )

)

人类可读的结果:

array_walk_recursive($result, 'applyFormat');

function applyFormat(&$item)
{
    $item = date('Y-m-d H:i:s', $item);
}

Array
(
    [0] => Array
        (
            [start] => 2014-12-15 00:00:00
            [end] => 2014-12-31 23:59:59
        )

    [1] => Array
        (
            [start] => 2015-01-01 00:00:00
            [end] => 2015-01-31 23:59:59
        )

    [2] => Array
        (
            [start] => 2015-02-01 00:00:00
            [end] => 2015-02-28 23:59:59
        )

    [3] => Array
        (
            [start] => 2015-03-01 00:00:00
            [end] => 2015-03-18 23:59:59
        )

)
于 2015-02-06T17:04:21.037 回答
2

这是一种非常基本的方法,但我认为对这样的事情进行调查可能会很有趣:

$date1 = '22-12-2013';
$date2 = "04-01-2014";

function split_date($d1, $d2)
{
    $start_year = substr($d1, -4);
    $end_year = substr($d2, -4);

    if($end_year > $start_year)
    {
    $d1 .= ' ['.date('d-m-Y',strtotime("last day of December".$start_year)).'] <br />';
    $d2 = '['.date('d-m-Y',strtotime("first day of January".$start_year)).'] ' . $d2;
    }
    return $d1 . $d2;
}

echo split_date($date1, $date2);

http://www.php.net/manual/en/datetime.formats.relative.php

于 2013-11-15T10:23:29.680 回答
0

感谢 ilpaijin,这就是我想出的:

$date1 = '22-12-2013';
$date2 = "04-01-2014";

function split_date($d1, $d2)
{
    $start_year = substr($d1, -4);
    $end_year = substr($d2, -4);

    if($end_year > $start_year)
    {
    $d1 .= ' ['.date('d-m-Y',strtotime("31-12-".$start_year)).'] <br />';
    $d2 = '['.date('d-m-Y',strtotime("01-01-".$start_year."+1 years")).'] ' . $d2;
    }
    return $d1 . $d2;
}

echo split_date($date1, $date2);
于 2013-11-15T10:57:38.353 回答
0

如果有人想将日期范围拆分为任意年,以下是实现。

$date1 = '22-12-2013';
$date2 = '04-01-2017';
$dA = array();

function split_date($d1, $d2, &$dA)
{
    $start_year = substr($d1, -4);
    $end_year = substr($d2, -4);

    if($end_year > $start_year)
    {
    $dA[] = $d1;    
        $dA[] = date('d-m-Y',strtotime("31-12-".$start_year));
        if($end_year - $start_year == 1)
        {
            $dA[] = date('d-m-Y',strtotime("01-01-".$start_year."+1 years"));
            $dA[] = $d2;
        }else
        {
            split_date(date('d-m-Y',strtotime("01-01-".$start_year."+1 years")), $d2, $dA);
        }
    }else if($end_year == $start_year)
    {
        $dA[] = $d1;
        $dA[] = $d2;
    }
}

split_date($date1, $date2, $dA);
print_r($dA);


//Answer

Array
(
[0] => 22-12-2013
[1] => 31-12-2013
[2] => 01-01-2014
[3] => 31-12-2014
[4] => 01-01-2015
[5] => 31-12-2015
[6] => 01-01-2016
[7] => 31-12-2016
[8] => 01-01-2017
[9] => 04-01-2017
)
于 2015-01-13T04:44:49.330 回答
0

非常简单的方法。您可以拆分日期范围支持所有类型的日期格式。

function split_date($start_date,$end_date){

    while($start_date < $end_date){
        $end = date("Y-m-d", strtotime("Last day of December", strtotime($start_date)));
        if($end_date<$end){
            $end = $end_date;
        }
        $dates[] =array('start'=>$start_date,'end'=>$end);

        $start_date =date("Y-m-d", strtotime("+1 day", strtotime($end)));

    }
    return $dates; 
}
$dates = split_date('2011-01-02','2013-01-04');
print_r($dates);

输出

Array
(
    [0] => Array
        (
            [start] => 2011-01-02   //start date
            [end] => 2011-12-31
        )

    [1] => Array
        (
            [start] => 2012-01-01
            [end] => 2012-12-31
        )

    [2] => Array
        (
            [start] => 2013-01-01
            [end] => 2013-01-04  //end date
        )

)
于 2017-03-07T10:41:16.713 回答