Simple php calendar with any day offset
Filed Under Php •
I am coding a quite huge calendar script for various works I have in progress.
The barebones of this calendar I have coded today represent a php function that returns a calendar into an html table of any month of any year (according to php limits) with any day offset.
This means your calendar could start on sunday, monday, tuesday and so on.
It's just the beginning but you can customize it with styles and other funny things.
Let's see the code.
-
function calendar($year, $month, $day_offset = 0){
-
$months = array("january","february","march","april","may","june","july","august","september","october","november","december");
-
$day_offset = $day_offset % 7;
-
$final_html .= "<table>\n<tr><td colspan = \"7\">".$months[$month-1]." $year</td></tr>\n";
-
for($x=0;$x<=6;$x++){
-
$final_html .= "<td>".$days[($x+$day_offset)%7]."</td>";
-
}
-
$final_html .= "</tr>\n";
-
$blank_days = $start_day_number - $day_offset;
-
for($x=0;$x<$blank_days;$x++){
-
$final_html .= "<td>x</td>";
-
}
-
for($x=1;$x<=$days_in_month;$x++){
-
if(($x+$blank_days-1)%7==0){
-
$final_html .= "</tr>\n<tr>";
-
}
-
$final_html .= "<td>$x</td>";
-
}
-
while((($days_in_month+$blank_days)%7)!=0){
-
$final_html .= "<td>x</td>";
-
$days_in_month++;
-
}
-
$final_html .= "</tr>\n</table>";
-
return($final_html);
-
}
Since it's just the beginning the code is quite simple, but if you have any question or suggestion, just leave a comment.
How will you use this function?
If you want the March 2006 calendar starting on Sunday, just
and the result is
| march 2006 | sunday | monday | tuesday | wednesday | thursday | friday | saturday | x | x | x | 1 | 2 | 3 | 4 |
| 5 | 6 | 7 | 8 | 9 | 10 | 11 |
| 12 | 13 | 14 | 15 | 16 | 17 | 18 |
| 19 | 20 | 21 | 22 | 23 | 24 | 25 |
| 26 | 27 | 28 | 29 | 30 | 31 | x |
While if you want April 2006 starting from monday, just
and the result is
| april 2006 | monday | tuesday | wednesday | thursday | friday | saturday | sunday | x | x | x | x | x | 1 | 2 |
| 3 | 4 | 5 | 6 | 7 | 8 | 9 |
| 10 | 11 | 12 | 13 | 14 | 15 | 16 |
| 17 | 18 | 19 | 20 | 21 | 22 | 23 |
| 24 | 25 | 26 | 27 | 28 | 29 | 30 |
GRAHAM Sent me a code he modified to include cycling through months and years and converted it to div’s making it xhtml compliant.
-
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
-
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
-
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
-
<title>Calender</title>
-
<style type="text/css">
-
ul.calender{
-
background-color: #CCCCCC;
-
margin: 0px;
-
width: 560px;
-
clear: both;
-
float: left;
-
border-top-width: 1px;
-
border-top-style: solid;
-
border-top-color: #F4F4F4;
-
font-size: 10px;
-
text-transform: uppercase;
-
list-style-type: none;
-
padding: 0px;
-
}
-
ul.calender li{
-
margin: 0px;
-
border-top: 1px solid #FFFFFF;
-
border-bottom: 1px solid #999999;
-
padding: 2px 0px 5px;
-
}
-
ul.calender2{
-
background-color: #FFFFFF;
-
margin: 0px;
-
width: 100%;
-
clear: both;
-
float: left;
-
font-size: 12px;
-
list-style-type: none;
-
padding: 0px;
-
font-weight: bold;
-
}
-
ul.calender2 li{
-
margin: 0px;
-
padding: 2px 0px 5px;
-
}
-
.nav1{
-
display: block;
-
width: 80px;
-
float: left;
-
text-align: center;
-
}
-
.nav2{
-
display: block;
-
width: 400px;
-
float: left;
-
text-align: center;
-
}
-
</style>
-
</head>
-
<?php
-
function calendar($year, $month, $day_offset = 0){
-
$days = array("sunday","monday","tuesday","wednesday","thursday","friday","saturday");
-
$months = array("january","february","march","april","may","june","july","august","september","october","november","december");
-
$day_offset = $day_offset % 7;
-
$start_day = gmmktime(0,0,0,$month,1,$year);
-
$start_day_number = date("w",$start_day);
-
$days_in_month = date("t",$start_day);
-
-
$previous_month = ($month-1);
-
$previous_year = $year;
-
if($previous_month == 0){
-
$previous_month = '12';
-
$previous_year = ($year-1);
-
}
-
-
$next_month = ($month+1);
-
$next_year = $year;
-
if($next_month == '13'){
-
$next_month = '1';
-
$next_year = ($year+1);
-
}
-
-
$final_html .= "<ul class=\"calender\"><li><span class=\"nav1\"><a href=\"calender.php?y=".$previous_year."&m=".$previous_month."\">Back</a></span><span class=\"nav2\">".$months[$month-1]." $year</span><span class=\"nav1\"><a href=\"calender.php?y=".$next_year."&m=".$next_month."\">Next</a></span></li>\n";
-
$final_html .= "<li>";
-
for($x=0;$x<=6;$x++){
-
}
-
$final_html .= "</li>\n";
-
-
-
-
$blank_days = $start_day_number - $day_offset;
-
if($blank_days<0){$blank_days = 7-abs($blank_days);}
-
for($x=0;$x<$blank_days;$x++){
-
$final_html .= "<span class=\"nav1\"> </span>";
-
}
-
for($x=1;$x<=$days_in_month;$x++){
-
if(($x+$blank_days-1)%7==0){
-
}
-
}
-
while((($days_in_month+$blank_days)%7)!=0){
-
$days_in_month++;
-
}
-
$final_html .= "</li>\n</ul>";
-
return($final_html);
-
}
-
-
if(isset($_GET['y'])){
-
$y = $_GET['y'];
-
} else {
-
$y = date("Y");
-
}
-
if(isset($_GET['m'])){
-
$m = $_GET['m'];
-
} else {
-
$m = date("m");
-
}
-
echo calendar($y,$m);
-
?>
-
</body>
-
</html>
Tell me what do you think about this post. I'll write better and better entries.
20 Responses to “Simple php calendar with any day offset”
Leave a Reply

(4 votes, average: 4.25 out of 5)
Please check line8 & line9
You open tag without any
ps. it’s a very good & simple function i’ve looking for! thanks a lot!
Love this function :) Keep up the good work!
Hi, this is a really neat function, but I don’t think it works quite right–try putting in July
echo calendar(2006,7,1);
and you’ll notice that it is one day of the week behind . . .
Hi Andrew, I tried but I got a correct result, with weekaday starting on monday and 1st on saturday
Hello, thankyou for the nice script, very simple to use. I’ve added some more functionality, back and next buttons to cycle through months/years. Plus i’ve converted it to XHTML compliant code using div’s and ul’s. Thought i’d send you a copy - hope you like.
Edited by Emanuele Feronato: Graham’s update is here.
Thank you very much.
Hi, the problem (for me, writing from the States) was the function gmmktime() in line 5–using mktime() makes the dates line up over here . . . .
Thanks again for posting the script.
Really nice one mister Feronato. This was the kind of less = more calendar script i was looking for.
Hi, i have a problem with February, this calendar say that the month has 31 days :s and that afect other months like oct, it say has a 30 days how can i fix that?
DATE FIX Ok, on line 5 you must change $start_day = gmmktime(0,0,0,$month,1,$year);
to this $start_day = mktime(0,0,0,$month, 1, $year); this will fix the days in the month issue. and show the correct date. http://marylandwebsites.net
great, this saved me a couple of hours work =)
Too bad the CSS is broken on all other browsers but IE :(
Thanks for this code.
You’ve saved me a lot of hours of work here too.
Working fine here (Brazil) with mktime instead of gmmktime
Excellent!
I prefer the emanuele version —with minor adjustements— is nice, simple & unbloatted. Tables are great for tabular data even in XHTML, so convert the table in div’s make no sense.
Excuses for kicking… I googled this page up.
Got something to report:
line 12: $final_html .= “\n”;
fix that into: $final_html .= “\n”;
cause on line 16 u start with some ’s and there is no to start it with.
bye, Rob.
Hi.
Best ever, honestly, saved me hours, Had only one problem, the gmmktime function seems to return a different result so had to use mktime, maybe because I am in australia or maybe because I am on my local machine.
Any way great stuff, thanks a lot.
This is what I need!
But add:
if($leap){
$day_offset–;
}
between line 11 and 12 to add Leap functionality.
Ruben
Oops…
I forget to add
$leap=date(”L”);
to my code…
Ruben
Excellent; just what I was looking for. Thank you :)
your a genius mister feronato
Just comes up with T_String error after $days array your line 2