如何查找给定月份的周日期范围

时间:2017-12-08 04:06:48

标签: php mysql date

我需要在选定月份的相应周内显示一系列日期。 假设所选值为$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,但这些都不适合我。

感谢。

4 个答案:

答案 0 :(得分:0)

这是您的解决方案

$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
        )

)

答案 1 :(得分:0)

试试这个:

<?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);

答案 2 :(得分:0)

这是一个用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

答案 3 :(得分:0)

此方法可以显示数周,包括打破该周的星期几

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