PHP将月份划分为周到天
我想把每个月分成几天,如图所示: 我写了一些代码,但我有这样的东西:PHP将月份划分为周到天,php,time,calendar,days,Php,Time,Calendar,Days,我想把每个月分成几天,如图所示: 我写了一些代码,但我有这样的东西: <?php $start_date = date('Y-m-d', strtotime('2015-12-28')); $end_date = date('Y-m-d', strtotime('2018-01-01')); $i=1; for($date = $start_date; $date <= $end_date; $date = date('Y-m-d', strtotime($date.
<?php
$start_date = date('Y-m-d', strtotime('2015-12-28'));
$end_date = date('Y-m-d', strtotime('2018-01-01'));
$i=1;
for($date = $start_date; $date <= $end_date; $date = date('Y-m-d', strtotime($date. ' + 7 days'))) {
echo getWeekDates($date, $start_date, $end_date, $i);
echo "\n";
$i++;
}
function getWeekDates($date, $start_date, $end_date, $i) {
$week = date('W', strtotime($date));
$year = date('Y', strtotime($date));
$from = date("Y-m-d", strtotime("{$year}-W{$week}+1"));
if($from < $start_date) $from = $start_date;
$to = date("Y-m-d", strtotime("{$year}-W{$week}-7"));
if($to > $end_date) $to = $end_date;
echo "$i. od ".$from." do ".$to.'<br>';
}
?>
我不知道如何排除例外情况,例如一个月中的一周有1天、2天、3天、4天、5天或6天……您可以使用/修改来实现您想要的
为了理解逻辑,对代码进行了大量的注释
这是您打印日历的方式
include 'calendar.php';
$calendar = new Calendar();
echo $calendar->show();
无论您做什么,在内部将日期表示为字符串都将是一件痛苦的事情
include 'calendar.php';
$calendar = new Calendar();
echo $calendar->show();