两个日期范围之间的分周

时间:2015-02-25 13:20:03

标签: php date datetime split

从两个日期范围逐周拆分。

例如:

开始日期:2015-01-11

截止日期:2015-01-31

需要这种格式:

第1周:

Array ( [0] => 2015-01-11 [1] => 2015-01-12 [2] => 2015-01-13 [3] => 15-01-14 [4] => 2015-01-15 [5] => 2015-01-16 [6] => 2015-01-17 

第2周:

Array([0] => 2015-01-18 [1] => 2015-01-19 [2] => 2015-01-20 [3] => 2015-01-21 [4] => 2015-01-22 [5] => 2015-01-23 [6] => 2015-01-24)

第3周:

Array([0] => 2015-01-25 [1] => 2015-01-26 [2] => 2015-01-27 [3] => 2015-01-28 [4] => 2015-01-29 [5] => 2015-01-30 [6] => 2015-01-31)

感谢提前。

1 个答案:

答案 0 :(得分:3)

这样的事情对你有用吗?

<?php
 // Set timezone
 date_default_timezone_set('UTC');

 // Start date
 $date = '2015-01-11';
 // End date
 $end_date = '2015-01-31';
 $array_final = array();
 $array  = array();
 $i=0;
 while (strtotime($date) <= strtotime($end_date)) {
     if($i%6==0 && $i>0){
         array_push($array_final,$array);   
         $array  = array();
     }

    array_push($array,$date);    

    $date = date ("Y-m-d", strtotime("+1 day", strtotime($date)));
    $i++;
}
print_r($array_final);
?>

它返回一个每周都有一行的数组。 每个弱行都返回你想要的东西:一个从0到6的数组,其中包含日期。

您可以在phpFiddle

上试用

数组(

[0] =&gt;数组([0] =&gt; 2015-01-11 [1] =&gt; 2015-01-12 [2] =&gt; 2015-01-13 [3] =&gt; 2015-01-14 [4] = &gt; 2015-01-15 [5] =&gt; 2015-01-16)

[1] =&gt;数组([0] =&gt; 2015-01-17 [1] =&gt; 2015-01-18 [2] =&gt; 2015-01-19 [3] =&gt; 2015-01-20 [4] = &gt; 2015-01-21 [5] =&gt; 2015-01-22)

[2] =&gt;数组([0] =&gt; 2015-01-23 [1] =&gt; 2015-01-24 [2] =&gt; 2015-01-25 [3] =&gt; 2015-01-26 [4] = &gt; 2015-01-27 [5] =&gt; 2015-01-28))