关于php:如何查找给定月份的星期日期范围

How to find week date ranges for a given month

本问题已经有最佳答案,请猛点这里访问。

我需要显示所选月份在相应星期中的日期范围。
假设选择的值是$month=2$year=2017
输出应显示所选的特定月份中数周的日期范围列表。

1
2
3
4
5
6
7
8
9
10
$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

我在php中查看了将当前月份拆分为几周,在PHP中获取了从日期开始的月份中的周数吗?和将日期划分为相应的星期,但没有一个对我有用。

谢谢。


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

显示测试

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
$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]}\
\
"
;
}

输出

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


这是一种以strtotime循环的方法。

我在Unix上添加86400 * 7来增加一周。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
$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) ."\
"
; // 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


尝试一下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<?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);

这是您的解决方案

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
$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"[cc lang="php"]";print_r($week);

输出

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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
        )

)