我试着用格式化的方式显示两次约会之间的周末。
举个例子:
$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:44:37
这应该可以做到:
$start = strtotime('2018-03-18');
$end = strtotime('2018-06-12');
for ($i = $start; $i <= $end; $i = strtotime("+1 day", $i)) {
if (date('w', $i) == 6) {
list ($currentDate, $currentMonth, $currentYear) = explode(' ', date('j F Y', $i));
$i = strtotime("+1 day", $i);
list ($nextDate, $nextMonth, $nextYear) = explode(' ', date('j F Y', $i));
if ($currentMonth == $nextMonth) {
echo 'Saturday and Sunday - ' . $currentMonth. ' ' . $currentDate . '-' . ($currentDate + 1) . ', ' . $currentYear . PHP_EOL;
continue;
}
echo 'Saturday - ' . $currentMonth . ' ' . $currentDate . ', ' . $currentYear . PHP_EOL;
echo 'Sunday - ' . $nextMonth . ' ' . $nextDate . ', ' . $nextYear . PHP_EOL;
continue;
} elseif (date('w', $i) == 0) {
echo 'Sunday - ' . date('F j, Y', $i) . PHP_EOL;
}
}发布于 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。修正了。
发布于 2018-03-20 10:33:10
试试这个:
$start = strtotime(date('Y-m-d'));
$end = strtotime('2018-06-12');
for ($i = $start; $i <= $end; $i = strtotime("+1 day", $i)) {
$date = date('D Y-m-d N', $i);
$n = (int)date('N', $i);
if ($n > 5) {
echo $date . '<hr>';
}
}日期(‘N’,$i)将给你工作日的编号(1-星期一,7-周日)
然后检查它是否大于5(6或7) (星期六或周日)
https://stackoverflow.com/questions/49381705
复制相似问题