php将日期范围分成2个独立的范围

时间:2013-11-15 09:14:57

标签: php date split

我想知道,如果符合某些标准,是否可以将给定的日期范围拆分为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

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

5 个答案:

答案 0 :(得分: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

答案 1 :(得分:2)

如果您想分割更多月份,可以使用此功能:

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
        )

)

答案 2 :(得分: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);

答案 3 :(得分: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
)

答案 4 :(得分: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);

<强> OUPTPUT

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
        )

)