获取指定月份的所有周,从周日到周六的周

我直接在下面尝试了这段代码。它很接近,但仍然找不到从每月 1 号到第一个星期六的周,然后是从周日到周六的每周,直到最后一个周日到月底。


function get_weeks($month, $year) {

    $weeks = array();


    $date = DateTime::createFromFormat('mY', $month.$year);

    $date->modify('first day of this month');


    $end = clone $date;

    $end->modify('last day of this month');

    $interval = DateInterval::createFromDateString('1 week');

    $period = new DatePeriod($date, $interval, $end);


    $counter = 1;

    foreach ($period as $dt) {

        $end_of_week = clone $dt;

        $end_of_week->modify('this Saturday');

        $weeks[] = sprintf("Week %u: %s - %s", 

            $counter,

            $dt->format('Y-m-d'),

            $end_of_week->format('Y-m-d')

        );      

        $counter++;

    }


    return $weeks;

}

$weeks = get_weeks('08', '2020');

print_r($weeks); 

它打印:


Array

(

[0] => Week 1: 2020-08-01 - 2020-08-01

[1] => Week 2: 2020-08-08 - 2020-08-08

[2] => Week 3: 2020-08-15 - 2020-08-15

[3] => Week 4: 2020-08-22 - 2020-08-22

[4] => Week 5: 2020-08-29 - 2020-08-29

)

但它应该打印:


Array

(

[0] => Week 1: 2020-08-01 - 2020-08-01 //correct for this month since 1st day of month and 1st Saturday of this month just happen to be on the same day

[1] => Week 2: 2020-08-02 - 2020-08-08 

[2] => Week 3: 2020-08-09 - 2020-08-15 

[3] => Week 4: 2020-08-16 - 2020-08-22 

[4] => Week 5: 2020-08-23 - 2020-08-29 

[5] => Week 6: 2020-08-30 - 2020-08-31 //last Sunday until end of month

)


30秒到达战场
浏览 94回答 2
2回答

慕丝7291255

我决定从算术角度而不是日期时间角度来完成这项任务。也许有一种我没有考虑的优雅的日期时间对象变异技术,但我不认为我的函数调用轻方法是可怕的。我相信我的变量命名将通过我的脚本提供足够的逻辑指导,但实际上,我通过变量/计算量增加给定周的开始和结束天数,并在整个月的所有天数都达到时终止循环。代表。代码:(演示)function get_weeks($month, $year) {&nbsp; &nbsp; $weeks = [];&nbsp; &nbsp; $ym = $year . '-' . $month;&nbsp; &nbsp; $final = date('t', strtotime($ym));&nbsp; &nbsp; $firstSat = date('d', strtotime("first Saturday of $ym"));&nbsp; &nbsp; $d = 1;&nbsp; &nbsp; $weekNo = 0;&nbsp; &nbsp; do {&nbsp; &nbsp; &nbsp; &nbsp; $weekEnd = $d === 1 ? $firstSat : min($final, $d + 6);&nbsp; &nbsp; &nbsp; &nbsp; $weeks[] = sprintf(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "Week %d: %s-%02d - %s-%02d",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ++$weekNo,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $ym,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $d,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $ym,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $weekEnd&nbsp; &nbsp; &nbsp; &nbsp; );&nbsp; &nbsp; &nbsp; &nbsp; $d = $weekEnd + 1;&nbsp; &nbsp; } while ($weekEnd < $final);&nbsp; &nbsp; return $weeks;}foreach (['01','02','03','04','05','06', '07', '08', '09', '10', '11', '12'] as $month) {&nbsp; &nbsp; echo var_export(get_weeks($month, '2020'), true) . "\n";}输出:array (&nbsp; 0 => 'Week 1: 2020-01-01 - 2020-01-04',&nbsp; 1 => 'Week 2: 2020-01-05 - 2020-01-11',&nbsp; 2 => 'Week 3: 2020-01-12 - 2020-01-18',&nbsp; 3 => 'Week 4: 2020-01-19 - 2020-01-25',&nbsp; 4 => 'Week 5: 2020-01-26 - 2020-01-31',)array (&nbsp; 0 => 'Week 1: 2020-02-01 - 2020-02-01',&nbsp; 1 => 'Week 2: 2020-02-02 - 2020-02-08',&nbsp; 2 => 'Week 3: 2020-02-09 - 2020-02-15',&nbsp; 3 => 'Week 4: 2020-02-16 - 2020-02-22',&nbsp; 4 => 'Week 5: 2020-02-23 - 2020-02-29',)array (&nbsp; 0 => 'Week 1: 2020-03-01 - 2020-03-07',&nbsp; 1 => 'Week 2: 2020-03-08 - 2020-03-14',&nbsp; 2 => 'Week 3: 2020-03-15 - 2020-03-21',&nbsp; 3 => 'Week 4: 2020-03-22 - 2020-03-28',&nbsp; 4 => 'Week 5: 2020-03-29 - 2020-03-31',)array (&nbsp; 0 => 'Week 1: 2020-04-01 - 2020-04-04',&nbsp; 1 => 'Week 2: 2020-04-05 - 2020-04-11',&nbsp; 2 => 'Week 3: 2020-04-12 - 2020-04-18',&nbsp; 3 => 'Week 4: 2020-04-19 - 2020-04-25',&nbsp; 4 => 'Week 5: 2020-04-26 - 2020-04-30',)array (&nbsp; 0 => 'Week 1: 2020-05-01 - 2020-05-02',&nbsp; 1 => 'Week 2: 2020-05-03 - 2020-05-09',&nbsp; 2 => 'Week 3: 2020-05-10 - 2020-05-16',&nbsp; 3 => 'Week 4: 2020-05-17 - 2020-05-23',&nbsp; 4 => 'Week 5: 2020-05-24 - 2020-05-30',&nbsp; 5 => 'Week 6: 2020-05-31 - 2020-05-31',)array (&nbsp; 0 => 'Week 1: 2020-06-01 - 2020-06-06',&nbsp; 1 => 'Week 2: 2020-06-07 - 2020-06-13',&nbsp; 2 => 'Week 3: 2020-06-14 - 2020-06-20',&nbsp; 3 => 'Week 4: 2020-06-21 - 2020-06-27',&nbsp; 4 => 'Week 5: 2020-06-28 - 2020-06-30',)array (&nbsp; 0 => 'Week 1: 2020-07-01 - 2020-07-04',&nbsp; 1 => 'Week 2: 2020-07-05 - 2020-07-11',&nbsp; 2 => 'Week 3: 2020-07-12 - 2020-07-18',&nbsp; 3 => 'Week 4: 2020-07-19 - 2020-07-25',&nbsp; 4 => 'Week 5: 2020-07-26 - 2020-07-31',)array (&nbsp; 0 => 'Week 1: 2020-08-01 - 2020-08-01',&nbsp; 1 => 'Week 2: 2020-08-02 - 2020-08-08',&nbsp; 2 => 'Week 3: 2020-08-09 - 2020-08-15',&nbsp; 3 => 'Week 4: 2020-08-16 - 2020-08-22',&nbsp; 4 => 'Week 5: 2020-08-23 - 2020-08-29',&nbsp; 5 => 'Week 6: 2020-08-30 - 2020-08-31',)array (&nbsp; 0 => 'Week 1: 2020-09-01 - 2020-09-05',&nbsp; 1 => 'Week 2: 2020-09-06 - 2020-09-12',&nbsp; 2 => 'Week 3: 2020-09-13 - 2020-09-19',&nbsp; 3 => 'Week 4: 2020-09-20 - 2020-09-26',&nbsp; 4 => 'Week 5: 2020-09-27 - 2020-09-30',)array (&nbsp; 0 => 'Week 1: 2020-10-01 - 2020-10-03',&nbsp; 1 => 'Week 2: 2020-10-04 - 2020-10-10',&nbsp; 2 => 'Week 3: 2020-10-11 - 2020-10-17',&nbsp; 3 => 'Week 4: 2020-10-18 - 2020-10-24',&nbsp; 4 => 'Week 5: 2020-10-25 - 2020-10-31',)array (&nbsp; 0 => 'Week 1: 2020-11-01 - 2020-11-07',&nbsp; 1 => 'Week 2: 2020-11-08 - 2020-11-14',&nbsp; 2 => 'Week 3: 2020-11-15 - 2020-11-21',&nbsp; 3 => 'Week 4: 2020-11-22 - 2020-11-28',&nbsp; 4 => 'Week 5: 2020-11-29 - 2020-11-30',)array (&nbsp; 0 => 'Week 1: 2020-12-01 - 2020-12-05',&nbsp; 1 => 'Week 2: 2020-12-06 - 2020-12-12',&nbsp; 2 => 'Week 3: 2020-12-13 - 2020-12-19',&nbsp; 3 => 'Week 4: 2020-12-20 - 2020-12-26',&nbsp; 4 => 'Week 5: 2020-12-27 - 2020-12-31',)

POPMUISE

我有一个函数,它根据特定月份和年份中的特定周数 (1-6) 以 DateTime 对象数组的形式提供周开始日期和结束日期。如果发生错误,该函数将返回 NULL。function datesWeekInMonth(int $monthWeekNumber, int $month, int $year):?array {&nbsp; //week from Sun to Sat !&nbsp;&nbsp; $sat = date_create('first Sat of '.$year.'-'.$month);&nbsp; if($monthWeekNumber < 1 OR $sat === false) return NULL;&nbsp; if($monthWeekNumber == 1) return [date_create($year.'-'.$month.'-01'),$sat];&nbsp; $lastDayOfMonth = date_create('last Day of '.$year.'-'.$month);&nbsp; for($i=1;$i<$monthWeekNumber;$i++) {&nbsp; &nbsp; $sun = (clone $sat)->modify('+1 Day');&nbsp; &nbsp; $sat->modify('next Sat');&nbsp; }&nbsp; if($sun > $lastDayOfMonth) return NULL;&nbsp; return [$sun,min($sat,$lastDayOfMonth)];}使用示例:$result = datesWeekInMonth(3,8,2020); //third week of August 2020var_dump($result);/*array(2) {&nbsp; [0]=>&nbsp; object(DateTime)#4 (3) {&nbsp; &nbsp; ["date"]=>&nbsp; &nbsp; string(26) "2020-08-09 00:00:00.000000"&nbsp; &nbsp; ["timezone_type"]=>&nbsp; &nbsp; int(3)&nbsp; &nbsp; ["timezone"]=>&nbsp; &nbsp; string(13) "Europe/Berlin"&nbsp; }&nbsp; [1]=>&nbsp; object(DateTime)#1 (3) {&nbsp; &nbsp; ["date"]=>&nbsp; &nbsp; string(26) "2020-08-15 00:00:00.000000"&nbsp; &nbsp; ["timezone_type"]=>&nbsp; &nbsp; int(3)&nbsp; &nbsp; ["timezone"]=>&nbsp; &nbsp; string(13) "Europe/Berlin"&nbsp; }}*/
打开App,查看更多内容
随时随地看视频慕课网APP