Php会在特定日期返回一组意外的周日期。例如,给定日期2015-05-20
,应返回的星期几是:
array:7 [
0 => "2015-05-17"
1 => "2015-05-18"
2 => "2015-05-19"
3 => "2015-05-20"
4 => "2015-05-21"
5 => "2015-05-22"
6 => "2015-05-23"
]
或以纯文本 2015年5月17日至23日。据我所知, 正确无误。现在,给定日期2015-05-17
,我得到以下结果:
array:7 [
0 => "2015-05-10"
1 => "2015-05-11"
2 => "2015-05-12"
3 => "2015-05-13"
4 => "2015-05-14"
5 => "2015-05-15"
6 => "2015-05-16"
]
您将注意到的是17日之前的一周,或 2015年5月11日至16日。
以下是拉我工作日的功能:
$days_of_week = array();
for($i = 0; $i <= 6; $i ++){
$days_of_week[] = date("Y-m-d", strtotime(date("o", strtotime($date))."W".date("W", strtotime($date)).$i));
}
基本上,对于给定周数中的7天(在这种情况下为20),将格式化的值推送到数组(如上面的两个示例所示。)预格式化,推送的值为:
2015W200
2015W201
2015W202
2015W203
2015W204
2015W205
2015W206
// Year: 2015, Week: 20, Day(In week): 0-6
我认为我面临的问题是ISO-8601周数从星期一开始到星期日结束,但是我试图从星期日开始一周,到星期六结束(在日历上显示)。 )还有另一种方法来实现这一目标吗?
答案 0 :(得分:0)
实际上这是预期的结果。
constructing child
copy-constructing child
destructing child
destructing child
constructing child
move-constructing child
destructing zombie
destructing child
constructing child
move-constructing child
destructing zombie
destructing child
是在第20周。
因此,天数和星期数分别为:(10 11,12,13,14,15,16)和(0,1,2,3,4,5,6) 请参阅format compounds:
中文档的本地化符号部分<强>符号强> ISO年份与ISO周和日:
2015-05-17
最后一个令牌保留一周的数字,从周日到周六开始。 因此,当您循环查看周数时:
YY "-"? "W" W "-"? [0-7]
因此,您必须指定正确的一周中的年份:
2015W20[0] -> That is, a date on sunday (2015-05-10)
2015W20[1] -> That is, a date on monday (2015-05-18)
答案 1 :(得分:0)
我想出了我的问题。忽略ISO-8601周数(1-53),我可以根据给定的日期周数(0周日 - 周六)获得7天的范围。
$day = new DateTime($date); // 2015-05-21
$start = $day->modify("- ".$day->format("w")." days"); // - 4 days - 2015-05-17
$end = clone $start;
$end = $start->modify("+ 7 days"); // 2015-05-24 (foreach ignores last day)
$interval = new DateInterval("P1D");
$week = new DatePeriod($start, $interval, $end);
foreach($week as $key => $val) {
$days_of_week[] = date("Y-m-d", strtotime($val->format("Y-m-d")));
}
此方法根据给定日期正确返回七天的数组,从星期日开始到星期六结束。感谢@ Rizier123作为这个答案的支柱。