我试着用格式化的方式显示两次约会之间的周末。
举个例子:
$start = strtotime(date('Y-m-d'));
$end = strtotime(2018-06-12);
for ($i = $start; $i <= $end; $i = strtotime("+1 day", $i)) {
//show weekends as
// saturday and sunday - march 24-25, 2018
// saturday - march 31, 2018
// sunday - april 1, 2018
// saturday and sunday - april 7-8, 2018
//.........
}如果你能看到上面,我需要分组周末,如果星期六和星期日是在两个不同的月份分别显示他们。
有人能帮我做这件事吗?
发布于 2018-03-20 10:56:03
下面是一个方法,它创建一个数组,稍后我可以将其内爆以获得所需的字符串格式。
数组构建在年、月、周和日上。
那就是迭代和回响的问题。
$start = strtotime(date('Y-m-d'));
$end = strtotime("2018-06-12");
for ($i = $start; $i <= $end;) {
If(date("N", $i) == 6){
$arr[date("Y", $i)][date("F", $i)][date("W", $i)][date("l", $i)] = date("d", $i);
$i+= 86400;
$arr[date("Y", $i)][date("F", $i)][date("W", $i)][date("l", $i)] = date("d", $i);
$i+= 86400*6;
}Else If(date("N", $i) == 7){
$arr[date("Y", $i)][date("F", $i)][date("W", $i)][date("l", $i)] = date("d", $i);
$i+= 86400*6;
}Else{
$i+= 86400;
}
}
Foreach($arr as $year => $years){
Foreach($years as $month => $months){
Foreach($months as $week){
Echo implode(" and ",array_keys($week)) . " - " . $month . " " . Implode("-", $week) . ", ". $year . "\n";
}
}
}编辑:忘记输出月份。
编辑2:将初始循环更改为“不循环”。应该会稍微快一点。
编辑3:在代码中发现了一个bug。修正了。
https://stackoverflow.com/questions/49381705
复制相似问题