I've dealt with creating monthly calendars in PHP and it has always been a bit of pain. How exactly to show the days in the month in a table without creating ugly and hard to maintain code? I believe this time I was able to find a good solution.
[0, 0, 1, 2, 3, 4, 5]
This allows you to loop through the returned arrays without worrying where to start the week. If you use a table, you could simply output " " when the date is 0 and output the number (and colored background) when the date is a number. Here is an example in HTML:
Easy, eh? We just output a table row for every week in the month ($num_weeks is also a method in the class as you'll see below) and for the days with date we output the date (otherwise just blank space).
You can see how it works live in this period calendar.
num_weeks($month, $year) - to calculate the number of weeks in the given month and year
first_day($month, $year) - to find out which day of the week is the first day of a given month.year
days($month, $year, $week, $num_weeks=0) - this is the main method that returns the array of 7 days. The week starts in Monday.
Here is the full code below and some comments to it:
Then the first_day function:
And here is the most important one:
That's it! Any questions?
A PHP Class
It's a PHP class that will output the dates of the given month in array. But in order to make the dates fit their place in the week, the class outputs zeros in the place of "empty" days. So the first week of a month starting in Wednesday would be this array:[0, 0, 1, 2, 3, 4, 5]
This allows you to loop through the returned arrays without worrying where to start the week. If you use a table, you could simply output " " when the date is 0 and output the number (and colored background) when the date is a number. Here is an example in HTML:
Using the output in HTML:
<table>
<tr><th>Mon</th><th>Tue</th><th>Wed</th><th>Thu</th><th>Fri</th><th>Sat</th><th>Sun</th></tr>
<?php for($i=1;$i<=$num_weeks;$i++):?>
<tr>
<?php $days=$_calendar->days($m[0], $m[1], $i, $num_weeks);
foreach($days as $day):?>
<td><?=$day?$day:" "?></td>
<?php endforeach;?>
</tr>
<?php endfor;?>
</table>
Easy, eh? We just output a table row for every week in the month ($num_weeks is also a method in the class as you'll see below) and for the days with date we output the date (otherwise just blank space).
You can see how it works live in this period calendar.
The PHP Code
Now, here is how it all works. I created a class to hold 3 functions:num_weeks($month, $year) - to calculate the number of weeks in the given month and year
first_day($month, $year) - to find out which day of the week is the first day of a given month.year
days($month, $year, $week, $num_weeks=0) - this is the main method that returns the array of 7 days. The week starts in Monday.
Here is the full code below and some comments to it:
function num_weeks($month, $year)
{
// every month has at least 4 weeks
$num_weeks=4;
// finding where it starts
$first_day = $this->first_day($month, $year);
// if the first week doesn't start on monday
// we are sure that the month has at minimum 5 weeks
if($first_day!=1) $num_weeks++;
// find the "widow" days (i.e. empty cells in the 1st week)
$widows=$first_day-1;
$fw_days=7-$widows;
if($fw_days==7) $fw_days=0;
// number of days in the month
$numdays=date("t",mktime(2, 0, 0, $month, 1, $year));
if( ($numdays - $fw_days) > 28 ) $num_weeks++;
// that's it!
return $num_weeks;
}
Then the first_day function:
function first_day($month, $year)
{
$first_day= date("w", mktime(2, 0, 0, $month, 1, $year));
if($first_day==0) $first_day=7; # convert Sunday
return $first_day;
}
And here is the most important one:
// here $week is the week number when we go in a loop
// (see the html code that I posted earlier to get better idea)
function days($month, $year, $week, $num_weeks=0)
{
$days=array();
// this is just to avoid calling num_weeks every time
// when you loop through the weeks
if($num_weeks==0) $num_weeks=$this->num_weeks($month, $year);
// find which day of the week is 1st of the given month
$first_day = $this->first_day($month, $year);
// find widow days (first week)
$widows=$first_day-1;
// first week days
$fw_days=7-$widows;
// if $week==1 don't do further calculations
if($week==1)
{
for($i=0;$i<$widows;$i++) $days[]=0;
for($i=1;$i<=$fw_days;$i++) $days[]=$i;
return $days;
}
// any other week
if($week!=$num_weeks)
{
$first=$fw_days+(($week-2)*7);
for($i=$first+1;$i<=$first+7;$i++) $days[]=$i;
return $days;
}
# only last week calculations below
// number of days in the month
$numdays=date("t",mktime(2, 0, 0, $month, 1, $year));
// find orphan days (last week)
$orphans=$numdays-$fw_days-(($num_weeks-2)*7);
$empty=7-$orphans;
for($i=($numdays-$orphans)+1;$i<=$numdays;$i++) $days[]=$i;
for($i=0;$i<$empty;$i++) $days[]=0;
return $days;
}
That's it! Any questions?
No comments:
Post a Comment