【问题标题】:How to find week date ranges for a given month [duplicate]如何查找给定月份的周日期范围[重复]
【发布时间】:2023-03-19 19:11:01
【问题描述】:

我需要显示所选月份对应周的日期范围。 假设选定的值为$month=2$year=2017。 输出应显示所选特定月份中几周的范围日期列表。

$month = $_GET['month'];
$year = $_GET['year'];
 ...

Output:
Week 1: 01/02/2017 - 05/02/2017
Week 2: 06/02/2017 - 12/02/2017
Week 3: 13/02/2017 - 19/02/2017
Week 4: 20/02/2017 - 26/02/2017
Week 5: 27/02/2017 - 28/02/2017

我查看了 Split the current month in to weeks in phpGet week number in month from date in PHP?Split date ranges into corresponding weeks ,但没有一个对我有用。

谢谢。

【问题讨论】:

    标签: php mysql date


    【解决方案1】:

    这是一种使用 strtotime 循环的方法。

    我在Unix上加86400*7加一周。

    $month = 2;
    $year = 2017;
    
    $week = date("W", strtotime($year . "-" . $month ."-01")); // weeknumber of first day of month
    
    Echo date("d/m/Y", strtotime($year . "-" . $month ."-01")) ." - "; // first day of month
    $unix = strtotime($year."W".$week ."+1 week");
    While(date("m", $unix) == $month){ // keep looping/output of while it's correct month
    
       Echo date("d/m/Y", $unix-86400) . "\n"; // Sunday of previous week
       Echo date("d/m/Y", $unix) ." - "; // this week's monday
       $unix = $unix + (86400*7);
    }
    Echo date("d/m/Y", strtotime("last day of ".$year . "-" . $month)); //echo last day of month
    

    https://3v4l.org/LAMBl

    【讨论】:

    • 谢谢!这真的对我有很大帮助..你的很容易理解..我欠你很多时间:)
    【解决方案2】:

    这是你的解决方案

    $month = $_GET['month'];
    if($month<10)$month = '0'.$month;
    $year = $_GET['year'];
    $days = cal_days_in_month(CAL_GREGORIAN,$month,$year);
    $start = 1; $end = 5;
    function get_week_array($start,$end){
        global $month, $year,$days;
        for($i=0;$i<$end;$i++){
            if($start<10)$array[] = '0'.$start.'/'.$month.'/'.$year;
            else $array[] = $start.'/'.$month.'/'.$year;
            $start = $start+1;
            if($start==$days+1)break;
        }
        return $array;
    }
    $total = 0;
    while($days>$total){
        $week[] = get_week_array($start,$end);
        $total = $total+$end;
        $start = $total+1;
        $end = 7;
    }
    echo "<pre>";print_r($week);
    

    输出

    Array
    (
        [0] => Array
            (
                [0] => 01/02/2017
                [1] => 02/02/2017
                [2] => 03/02/2017
                [3] => 04/02/2017
                [4] => 05/02/2017
            )
    
        [1] => Array
            (
                [0] => 06/02/2017
                [1] => 07/02/2017
                [2] => 08/02/2017
                [3] => 09/02/2017
                [4] => 10/02/2017
                [5] => 11/02/2017
                [6] => 12/02/2017
            )
    
        [2] => Array
            (
                [0] => 13/02/2017
                [1] => 14/02/2017
                [2] => 15/02/2017
                [3] => 16/02/2017
                [4] => 17/02/2017
                [5] => 18/02/2017
                [6] => 19/02/2017
            )
    
        [3] => Array
            (
                [0] => 20/02/2017
                [1] => 21/02/2017
                [2] => 22/02/2017
                [3] => 23/02/2017
                [4] => 24/02/2017
                [5] => 25/02/2017
                [6] => 26/02/2017
            )
    
        [4] => Array
            (
                [0] => 27/02/2017
                [1] => 28/02/2017
            )
    
    )
    

    【讨论】:

    • 看不懂你...请解释清楚....
    • @Lynn 每个子数组的最小值和最大值。但通常答案应该让 cmets 解释代码的作用以及如何使用它。
    【解决方案3】:

    试试这个:

    <?php
    
    $month = 12;
    $year = 2017;
    
    $date = new DateTime($year.'-'.$month.'-01');
    
    $count = 1;
    $week[$count]['start'] =  $date->format('Y-m-d');
    
    while (1) {
        $date->modify('+1 day');
        if ($date->format('m') != $month) {
            $week[$count]['end'] = $date->format('Y-m-d');
            break;
        }
    
        if ($date->format('D') === 'Sun') {
            $week[$count++]['end'] = $date->format('Y-m-d');
        }
    
        if ($date->format('D') === 'Mon') {
            $week[$count]['start'] = $date->format('Y-m-d');
        }
    
    }
    
    
    print_r($week);
    

    【讨论】:

      【解决方案4】:

      此方法可以显示周数,包括该周的休息日

      Show test

      $month = '2';
      $year = '2017';
      $lastDayOfWeek = '7'; //1 (for monday) to 7 (for sunday)
      
      function getWeeksInMonth($year, $month, $lastDayOfWeek)
      {
          $aWeeksOfMonth = [];
          $date = new DateTime("{$year}-{$month}-01");
          $iDaysInMonth = cal_days_in_month(CAL_GREGORIAN, $month, $year);
          $aOneWeek = [$date->format('Y-m-d')];
          $weekNumber = 1;
          for ($i = 1; $i <= $iDaysInMonth; $i++)
          {
              if ($lastDayOfWeek == $date->format('N') || $i == $iDaysInMonth)
              {
                  $aOneWeek[]      = $date->format('Y-m-d');
                  $aWeeksOfMonth[$weekNumber++] = $aOneWeek;
                  $date->add(new DateInterval('P1D'));
                  $aOneWeek = [$date->format('Y-m-d')];
                  $i++;
      
              }
              $date->add(new DateInterval('P1D'));
          }
          return $aWeeksOfMonth;
      }
      
      $weeks = getWeeksInMonth($year, $month, $lastDayOfWeek);
      
      foreach($weeks as $weekNumber => $week){
          echo "Week {$weekNumber}: {$week[0]} - {$week[1]}\r\n";
      }
      

      输出

      第 1 周:2017/02/01 - 2017/02/05
      第 2 周:2017/02/06 - 2017/02/12
      第 3 周:2017/02/13 - 2017/02/19
      第 4 周:2017/02/20 - 2017/02/26
      第 5 周:2017/02/27 - 2017/02/28

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-03-23
        • 2012-09-24
        • 2021-11-09
        • 2022-10-23
        • 1970-01-01
        • 2012-02-05
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多