星期一到第n周的日期

时间:2017-06-10 18:55:12

标签: php

我有一系列日期。由于银行假日或其他日期,这些日期的某些星期一可能会丢失,因此我将获得代表该周的下一个可用日期。

$array = array("May 30 2017","May 29 2017","May 28 2017","May 26 2017","May 25 2017","May 24 2017","May 23 2017","May 19 2017","May 18 2017","May 17 2017","May 16 2017","May 15 2017","May 12 2017","May 11 2017","May 10 2017","May 09 2017","May 08 2017","May 05 2017","May 04 2017","May 03 2017","May 02 2017","May 01 2017","Apr 28 2017","Apr 27 2017","Apr 26 2017","Apr 25 2017","Apr 24 2017","Apr 21 2017","Apr 20 2017","Apr 19 2017","Apr 18 2017","Apr 17 2017","Apr 14 2017","Apr 13 2017","Apr 12 2017","Apr 11 2017","Apr 10 2017","Apr 07 2017","Apr 06 2017","Apr 05 2017","Apr 04 2017","Apr 03 2017","Mar 31 2017","Mar 30 2017","Mar 29 2017");

我最终想要这样做2周,3周等等,所以我添加了参数w

if(isset($_GET['w'])) {
        $week_loop = $_GET['w']*5;
}


if(isset($_GET['w'])) {
 foreach($array as $date) {
    $array_date = date("D",strtotime($date));


        if($array_date=="Mon") {
                echo "<p>Mon - ".date("j/m/y",strtotime($date))." - count($i)</p>";
                $i = 0;
        } 


     }

     $i++;

}

我有问题进展,到目前为止我只显示阵列中的所有星期一。我如何将每2周,3周等时间纳入循环并获得该周的第一个可用日期?

1 个答案:

答案 0 :(得分:1)

解决方案的评论是为了解释:

<?php

$array = array("May 30 2017","May 29 2017","May 28 2017","May 26 2017","May 25 2017","May 24 2017","May 23 2017","May 19 2017","May 18 2017","May 17 2017","May 16 2017","May 15 2017","May 12 2017","May 11 2017","May 10 2017","May 09 2017","May 08 2017","May 05 2017","May 04 2017","May 03 2017","May 02 2017","May 01 2017","Apr 28 2017","Apr 27 2017","Apr 26 2017","Apr 25 2017","Apr 24 2017","Apr 21 2017","Apr 20 2017","Apr 19 2017","Apr 18 2017","Apr 17 2017","Apr 14 2017","Apr 13 2017","Apr 12 2017","Apr 11 2017","Apr 10 2017","Apr 07 2017","Apr 06 2017","Apr 05 2017","Apr 04 2017","Apr 03 2017","Mar 31 2017","Mar 30 2017","Mar 29 2017");

// convert these to DateTime objects
$array = array_map(function($date){
    return new DateTime($date);
}, $array);

// sort them chronologically
sort($array);

// chunk array into weeks
foreach ($array as $date) {
    $weeks[$date->format("W")] []= $date;
}

// optional filter by week group (e.g. ?w=2 or ?w=3)
if (isset($_GET['w'])) {
    $week_loop = $_GET['w'];
} else {
    $week_loop = 1;
}

// echo first day of each week
$count = 1;
foreach ($weeks as $number => $week) {
    if ($week_loop - $count === 0) {
        echo "<p>week: $number - {$week[0]->format("D")} - {$week[0]->format("j/m/y")}</p>\n";
        $count = 1;
    } else {
        $count++;
    }
}

没有参数,每周打印:

  

周:13 - 周三 - 2017年3月29日

     

周:14 - 周一至3月3日/ 17日

     

周:15 - 星期一 - 10/04/17

     

周:16 - 周一至17/04/17

     

周:17 - 星期一 - 24/04/17

     

周:18 - 周一至1/05/17

     

周:19 - 星期一 - 8/05/17

     

周:20 - 周一至15/05/17

     

周:21 - 星期二 - 23/05/17

     

周:22 - 星期一 - 29/05/17

给出?w=2

  

周:14 - 周一至3月3日/ 17日

     

周:16 - 周一至17/04/17

     

周:18 - 周一至1/05/17

     

周:20 - 周一至15/05/17

     

周:22 - 星期一 - 29/05/17

给出?w=3

  

周:15 - 星期一 - 10/04/17

     

周:18 - 周一至1/05/17

     

周:21 - 星期二 - 23/05/17