Simple php calendar with any day offset

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.

PHP:
  1. function calendar($year, $month, $day_offset = 0){
  2.     $days = array("sunday","monday","tuesday","wednesday","thursday","friday","saturday");
  3.     $months = array("january","february","march","april","may","june","july","august","september","october","november","december");
  4.     $day_offset = $day_offset % 7;
  5.     $start_day = gmmktime(0,0,0,$month,1,$year);
  6.     $start_day_number = date("w",$start_day);
  7.     $days_in_month = date("t",$start_day);
  8.     $final_html .= "<table>\n<tr><td colspan = \"7\">".$months[$month-1]." $year</td></tr>\n";
  9.     for($x=0;$x<=6;$x++){
  10.         $final_html .= "<td>".$days[($x+$day_offset)%7]."</td>";
  11.     }
  12.     $final_html .= "</tr>\n";
  13.     $blank_days = $start_day_number - $day_offset;
  14.     if($blank_days<0){$blank_days = 7-abs($blank_days);}
  15.     for($x=0;$x<$blank_days;$x++){
  16.         $final_html .= "<td>x</td>";
  17.     }
  18.     for($x=1;$x<=$days_in_month;$x++){
  19.         if(($x+$blank_days-1)%7==0){
  20.             $final_html .= "</tr>\n<tr>";
  21.         }
  22.         $final_html .= "<td>$x</td>";
  23.     }
  24.     while((($days_in_month+$blank_days)%7)!=0){
  25.         $final_html .= "<td>x</td>";
  26.         $days_in_month++;
  27.     }
  28.     $final_html .= "</tr>\n</table>";
  29.     return($final_html);
  30. }

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

PHP:
  1. echo calendar(2006,3);

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

PHP:
  1. echo calendar(2006,4,1);

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.

Here it is

HTML:
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  3. <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
  4. <title>Calender</title>
  5. <style type="text/css">
  6. ul.calender{
  7.     background-color: #CCCCCC;
  8.     margin: 0px;
  9.     width: 560px;
  10.     clear: both;
  11.     float: left;
  12.     border-top-width: 1px;
  13.     border-top-style: solid;
  14.     border-top-color: #F4F4F4;
  15.     font-size: 10px;
  16.     text-transform: uppercase;
  17.     list-style-type: none;
  18.     padding: 0px;
  19. }
  20. ul.calender li{
  21.     margin: 0px;
  22.     border-top: 1px solid #FFFFFF;
  23.     border-bottom: 1px solid #999999;
  24.     padding: 2px 0px 5px;
  25. }
  26. ul.calender2{
  27.     background-color: #FFFFFF;
  28.     margin: 0px;
  29.     width: 100%;
  30.     clear: both;
  31.     float: left;
  32.     font-size: 12px;
  33.     list-style-type: none;
  34.     padding: 0px;
  35.     font-weight: bold;
  36. }
  37. ul.calender2 li{
  38.     margin: 0px;
  39.     padding: 2px 0px 5px;
  40. }
  41. .nav1{
  42.     display: block;
  43.     width: 80px;
  44.     float: left;
  45.     text-align: center;
  46. }
  47. .nav2{
  48.     display: block;
  49.     width: 400px;
  50.     float: left;
  51.     text-align: center;
  52. }
  53. </style>
  54. </head>
  55. <?php
  56. function calendar($year, $month, $day_offset = 0){
  57.     $days = array("sunday","monday","tuesday","wednesday","thursday","friday","saturday");
  58.     $months = array("january","february","march","april","may","june","july","august","september","october","november","december");
  59.     $day_offset = $day_offset % 7;
  60.     $start_day = gmmktime(0,0,0,$month,1,$year);
  61.     $start_day_number = date("w",$start_day);
  62.     $days_in_month = date("t",$start_day);
  63.    
  64.     $previous_month = ($month-1);
  65.     $previous_year = $year;
  66.     if($previous_month == 0){
  67.       $previous_month = '12';
  68.       $previous_year = ($year-1);
  69.     }
  70.    
  71.     $next_month = ($month+1);
  72.     $next_year = $year;
  73.     if($next_month == '13'){
  74.       $next_month = '1';
  75.       $next_year = ($year+1);
  76.     }
  77.    
  78.     $final_html .= "<ul class=\"calender\"><li><span class=\"nav1\"><a href=\"calender.php?y=".$previous_year."&amp;m=".$previous_month."\">Back</a></span><span class=\"nav2\">".$months[$month-1]." $year</span><span class=\"nav1\"><a href=\"calender.php?y=".$next_year."&amp;m=".$next_month."\">Next</a></span></li>\n";
  79.     $final_html .= "<li>";
  80.     for($x=0;$x<=6;$x++){
  81.         $final_html .= "<span class=\"nav1\">".$days[($x+$day_offset)%7]."</span>";
  82.     }
  83.     $final_html .= "</li>\n";
  84.    
  85.     $final_html .= "</ul><ul class=\"calender2\"><li>\n";
  86.    
  87.    
  88.     $blank_days = $start_day_number - $day_offset;
  89.     if($blank_days<0){$blank_days = 7-abs($blank_days);}
  90.     for($x=0;$x<$blank_days;$x++){
  91.         $final_html .= "<span class=\"nav1\">&nbsp;</span>";
  92.     }
  93.     for($x=1;$x<=$days_in_month;$x++){
  94.         if(($x+$blank_days-1)%7==0){
  95.             $final_html .= "</li>\n<li>";
  96.         }
  97.         $final_html .= "<span class=\"nav1\"><a href=\"javascript:;\">$x</a></span>";
  98.     }
  99.     while((($days_in_month+$blank_days)%7)!=0){
  100.         $final_html .= "<span class=\"nav1\">&nbsp;</span>";
  101.         $days_in_month++;
  102.     }
  103.     $final_html .= "</li>\n</ul>";
  104.     return($final_html);
  105. }
  106.  
  107. if(isset($_GET['y'])){
  108.     $y = $_GET['y'];
  109. } else {
  110.     $y = date("Y");
  111. }
  112. if(isset($_GET['m'])){
  113.     $m = $_GET['m'];
  114. } else {
  115.     $m = date("m");
  116. }
  117. echo calendar($y,$m);
  118. ?>
  119. </body>
  120. </html>

Improve the blog rating this post
Tell me what do you think about this post. I'll write better and better entries.
1 Star2 Stars3 Stars4 Stars5 Stars (4 votes, average: 4.25 out of 5)
Loading ... Loading ...

19 Responses to “Simple php calendar with any day offset”

  1. Henry on July 6th, 2006 10:52 am

    Please check line8 & line9
    You open tag without any

    ps. it’s a very good & simple function i’ve looking for! thanks a lot!

  2. Juan on July 11th, 2006 10:12 pm

    Love this function :) Keep up the good work!

  3. Andrew on July 13th, 2006 6:32 pm

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

  4. Emanuele Feronato on July 14th, 2006 1:53 am

    Hi Andrew, I tried but I got a correct result, with weekaday starting on monday and 1st on saturday

  5. Graham on July 18th, 2006 1:18 pm

    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.

  6. Andrew on July 18th, 2006 8:02 pm

    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.

  7. Joost Canters on September 5th, 2006 12:27 pm

    Really nice one mister Feronato. This was the kind of less = more calendar script i was looking for.

  8. Juan on October 10th, 2006 2:38 am

    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?

  9. Mike on October 22nd, 2006 3:25 am

    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

  10. swahalla on November 9th, 2006 11:20 pm

    great, this saved me a couple of hours work =)

  11. niabi on December 20th, 2006 8:07 am

    Too bad the CSS is broken on all other browsers but IE :(

  12. Marcos J Pinto on May 6th, 2007 5:51 pm

    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

  13. augusto on August 20th, 2007 9:50 pm

    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.

  14. Rob on February 7th, 2008 10:33 pm

    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.

  15. agbagbara on April 3rd, 2008 1:20 am

    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.

  16. Ruben De Smet on June 11th, 2008 4:52 pm

    This is what I need!

    But add:

    if($leap){
    $day_offset–;
    }

    between line 11 and 12 to add Leap functionality.

    Ruben

  17. Ruben De Smet on June 11th, 2008 4:53 pm

    Oops…
    I forget to add
    $leap=date(”L”);
    to my code…

    Ruben

  18. Sakke on June 23rd, 2008 12:56 pm

    Excellent; just what I was looking for. Thank you :)

  19. cocoy on June 24th, 2008 7:05 pm

    your a genius mister feronato

Leave a Reply