Php 分组显示两个日期之间的所有周末
我试图以格式化的方式显示两次约会之间的周末 例如:Php 分组显示两个日期之间的所有周末,php,Php,我试图以格式化的方式显示两次约会之间的周末 例如: $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
$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
//.........
}
如果你能看到上面我需要分组的周末,如果周六和周日在两个不同的月份,分别显示他们
有人能帮我怎么做吗?试试这个:
$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>';
}
}
date'N',$我会给你们工作日的号码,周一,7-周日
然后检查周六或周日是否大于56或7
$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;
}
}
这里有一个方法,它创建了一个数组,稍后我可以将其内爆以获得所需的字符串格式。 阵列按年、月、周和日构建。 然后,这只是一个迭代和响应的问题
$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:在代码中发现错误。更正。如果星期天开始怎么办?只输出星期天还是跳过那个周末?星期六和星期日-2018年3月25日至28日,星期六和星期日为两天。你说25-28,那是四天。@Andreas如果是星期天,我只需要显示星期天。。有一个小错误。。应该是2018年3月24日至25日的星期六和星期日我编辑了它