Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP将月份划分为周到天_Php_Time_Calendar_Days - Fatal编程技术网

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();