[英]PHP Dividing the month into weeks to days
I want to divide every month into days like on picture: 我想将每个月分成几天,如图所示:
I write some code, but I've got something like this: 我写了一些代码,但是我有这样的东西:
<?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>';
}
?>
output: 输出:
1. od 2015-12-28 do 2016-01-03
2. od 2016-01-04 do 2016-01-10
3. od 2016-01-11 do 2016-01-17
4. od 2016-01-18 do 2016-01-24
I don't know how to exclude exeptions like week in the month has 1,2,3,4,5 or 6 days.... 我不知道如何排除像一个月中的一周有1,2,3,4,5或6天之类的...
You can use/modify this class to achieve what you want. 您可以使用/修改此类来实现您想要的。
The code is pretty much commented to understand the logic. 该代码已注释很多,以了解其逻辑。
This is how you can print the calendar. 这样可以打印日历。
include 'calendar.php';
$calendar = new Calendar();
echo $calendar->show();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.