简体   繁体   中英

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....

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM