2 回答
TA贡献1859条经验 获得超6个赞
我决定从算术角度而不是日期时间角度来完成这项任务。
也许有一种我没有考虑的优雅的日期时间对象变异技术,但我不认为我的函数调用轻方法是可怕的。
我相信我的变量命名将通过我的脚本提供足够的逻辑指导,但实际上,我通过变量/计算量增加给定周的开始和结束天数,并在整个月的所有天数都达到时终止循环。代表。
代码:(演示)
function get_weeks($month, $year) {
$weeks = [];
$ym = $year . '-' . $month;
$final = date('t', strtotime($ym));
$firstSat = date('d', strtotime("first Saturday of $ym"));
$d = 1;
$weekNo = 0;
do {
$weekEnd = $d === 1 ? $firstSat : min($final, $d + 6);
$weeks[] = sprintf(
"Week %d: %s-%02d - %s-%02d",
++$weekNo,
$ym,
$d,
$ym,
$weekEnd
);
$d = $weekEnd + 1;
} while ($weekEnd < $final);
return $weeks;
}
foreach (['01','02','03','04','05','06', '07', '08', '09', '10', '11', '12'] as $month) {
echo var_export(get_weeks($month, '2020'), true) . "\n";
}
输出:
array (
0 => 'Week 1: 2020-01-01 - 2020-01-04',
1 => 'Week 2: 2020-01-05 - 2020-01-11',
2 => 'Week 3: 2020-01-12 - 2020-01-18',
3 => 'Week 4: 2020-01-19 - 2020-01-25',
4 => 'Week 5: 2020-01-26 - 2020-01-31',
)
array (
0 => 'Week 1: 2020-02-01 - 2020-02-01',
1 => 'Week 2: 2020-02-02 - 2020-02-08',
2 => 'Week 3: 2020-02-09 - 2020-02-15',
3 => 'Week 4: 2020-02-16 - 2020-02-22',
4 => 'Week 5: 2020-02-23 - 2020-02-29',
)
array (
0 => 'Week 1: 2020-03-01 - 2020-03-07',
1 => 'Week 2: 2020-03-08 - 2020-03-14',
2 => 'Week 3: 2020-03-15 - 2020-03-21',
3 => 'Week 4: 2020-03-22 - 2020-03-28',
4 => 'Week 5: 2020-03-29 - 2020-03-31',
)
array (
0 => 'Week 1: 2020-04-01 - 2020-04-04',
1 => 'Week 2: 2020-04-05 - 2020-04-11',
2 => 'Week 3: 2020-04-12 - 2020-04-18',
3 => 'Week 4: 2020-04-19 - 2020-04-25',
4 => 'Week 5: 2020-04-26 - 2020-04-30',
)
array (
0 => 'Week 1: 2020-05-01 - 2020-05-02',
1 => 'Week 2: 2020-05-03 - 2020-05-09',
2 => 'Week 3: 2020-05-10 - 2020-05-16',
3 => 'Week 4: 2020-05-17 - 2020-05-23',
4 => 'Week 5: 2020-05-24 - 2020-05-30',
5 => 'Week 6: 2020-05-31 - 2020-05-31',
)
array (
0 => 'Week 1: 2020-06-01 - 2020-06-06',
1 => 'Week 2: 2020-06-07 - 2020-06-13',
2 => 'Week 3: 2020-06-14 - 2020-06-20',
3 => 'Week 4: 2020-06-21 - 2020-06-27',
4 => 'Week 5: 2020-06-28 - 2020-06-30',
)
array (
0 => 'Week 1: 2020-07-01 - 2020-07-04',
1 => 'Week 2: 2020-07-05 - 2020-07-11',
2 => 'Week 3: 2020-07-12 - 2020-07-18',
3 => 'Week 4: 2020-07-19 - 2020-07-25',
4 => 'Week 5: 2020-07-26 - 2020-07-31',
)
array (
0 => 'Week 1: 2020-08-01 - 2020-08-01',
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',
)
array (
0 => 'Week 1: 2020-09-01 - 2020-09-05',
1 => 'Week 2: 2020-09-06 - 2020-09-12',
2 => 'Week 3: 2020-09-13 - 2020-09-19',
3 => 'Week 4: 2020-09-20 - 2020-09-26',
4 => 'Week 5: 2020-09-27 - 2020-09-30',
)
array (
0 => 'Week 1: 2020-10-01 - 2020-10-03',
1 => 'Week 2: 2020-10-04 - 2020-10-10',
2 => 'Week 3: 2020-10-11 - 2020-10-17',
3 => 'Week 4: 2020-10-18 - 2020-10-24',
4 => 'Week 5: 2020-10-25 - 2020-10-31',
)
array (
0 => 'Week 1: 2020-11-01 - 2020-11-07',
1 => 'Week 2: 2020-11-08 - 2020-11-14',
2 => 'Week 3: 2020-11-15 - 2020-11-21',
3 => 'Week 4: 2020-11-22 - 2020-11-28',
4 => 'Week 5: 2020-11-29 - 2020-11-30',
)
array (
0 => 'Week 1: 2020-12-01 - 2020-12-05',
1 => 'Week 2: 2020-12-06 - 2020-12-12',
2 => 'Week 3: 2020-12-13 - 2020-12-19',
3 => 'Week 4: 2020-12-20 - 2020-12-26',
4 => 'Week 5: 2020-12-27 - 2020-12-31',
)
TA贡献1765条经验 获得超5个赞
我有一个函数,它根据特定月份和年份中的特定周数 (1-6) 以 DateTime 对象数组的形式提供周开始日期和结束日期。如果发生错误,该函数将返回 NULL。
function datesWeekInMonth(int $monthWeekNumber, int $month, int $year):?array {
//week from Sun to Sat !
$sat = date_create('first Sat of '.$year.'-'.$month);
if($monthWeekNumber < 1 OR $sat === false) return NULL;
if($monthWeekNumber == 1) return [date_create($year.'-'.$month.'-01'),$sat];
$lastDayOfMonth = date_create('last Day of '.$year.'-'.$month);
for($i=1;$i<$monthWeekNumber;$i++) {
$sun = (clone $sat)->modify('+1 Day');
$sat->modify('next Sat');
}
if($sun > $lastDayOfMonth) return NULL;
return [$sun,min($sat,$lastDayOfMonth)];
}
使用示例:
$result = datesWeekInMonth(3,8,2020); //third week of August 2020
var_dump($result);
/*
array(2) {
[0]=>
object(DateTime)#4 (3) {
["date"]=>
string(26) "2020-08-09 00:00:00.000000"
["timezone_type"]=>
int(3)
["timezone"]=>
string(13) "Europe/Berlin"
}
[1]=>
object(DateTime)#1 (3) {
["date"]=>
string(26) "2020-08-15 00:00:00.000000"
["timezone_type"]=>
int(3)
["timezone"]=>
string(13) "Europe/Berlin"
}
}
*/
- 2 回答
- 0 关注
- 93 浏览
添加回答
举报