A simple PHP calendar function

Recently I needed a calendar to display on a site. I looked around for something I could just cut and paste and drop in but I couldn’t find anything, but picked up something I had started about six months earlier and which had then fallen by the wayside. Now calendars aren’t actually very complicated, but they are a bit of a fiddle. The aim of this calendar function is therefore just to produce a skeleton calendar that I can drop into anything, you can stick anything you want in a calendar cell (day) – it doesn’t matter because all the code for content lives outside the calendar. There are css hooks for all the various cells, columns etc. so you can format it exactly how you want.

Calendar Screenshot

I wrote this as a CakePHP helper, but at the end of the day it is just a function and there is nothing Cake specific about it.

';
			
			$str .= '';
			
			$str .= 'prev' . ucfirst($month) . ' ' . $year . 'next';
			
			
			
			$str .= '';
				for($i = 0; $i < 7;$i++)
					{
						$str .= '' . $day_list[$i] . '';
					}
			$str .= '';
			
			$str .= '';
			
			// get the first day of the month
			
			
			$str .= '';
		
			
				
				while($day < $days_in_month)
					{
						$str .= '';
						
						
								
								for($i = 0; $i < 7; $i ++)
									{
									
										$cell = ' ';
										
										if(isset($data[$day]))
											{
												$cell = $data[$day];
											}
											
										$class = '';
										
										if($i > 4)
											{
												$class = ' class="cell-weekend" ';
											}
											
										
										if($day == $today)
											{
												$class = ' class="cell-today" ';
											}
									
										if(($first_day_in_month == $day_list[$i] || $day > 1) && ($day < $days_in_month))
											{
												$str .= '
' . $day . '
' . $cell . '
'; $day++; } else { $str .= ' '; } } $str .= ''; } $str .= ''; $str .= ''; return $str; } ?>

The function parameters are as follows:

  • $year – expects a year in 4 digit e.g. 2007
  • $month – expects a month in english e.g. january
  • $data – an array containing the data for each day of the month e.g.

    $data[2] = ‘This is an entry for the 2nd day of the month’;
    $data[24] = ‘A link for the 24th of the month‘;

    The data is any HTML you want – it is up to you to generate it yourself before you hand it to the calendar.

  • $base_url – the url to send the back / foward links on to i.e. the address of page (the calendar expects to be in a mod re-written situation e.g. www.flipflops.org/calendar/2008/june)

CSS

/* calendar CSS */

table.calendar {width: auto; border: 1px solid #cccccc; border-collapse: collapse; margin: 0px; padding: 0px; background-color: #ffffff;}
table.calendar th {background-color: #eeeeee; text-transform: none; color: #444444; padding: 4px; text-align: center; border: 1px solid #eeeeee;}

table.calendar th.cell-prev {text-align: left;}
table.calendar th.cell-next {text-align: right;}
table.calendar th.cell-header {width: 70px; border-bottom: 1px solid #cccccc;}
table.calendar td.cell-today {background-color: #e2e8f6;} /* today in the current month */
table.calendar td.cell-weekend {background-color: #F3F5EB;}
table.calendar td {border: 1px solid #cccccc;}
					
table.calendar td div.cell-number {text-align: right; font-size: 8px; color: #444444; display: block;}
table.calendar td div {display: block; font-size: 10px; text-align: left;}
table.calendar thead th {border: 1px solid #cccccc;}

I use this as a CakePHP helper – if anyone is interested I’ll put up the supporting code showing how it might be used in a view / controller situation.

16 thoughts to “A simple PHP calendar function”

  1. It’s OK – but it does exactly what I want – of course depending on your situation a lot of the code is superfluous – i.e. in a Cake situation – I can ensure that the month and year passed are valid – it becomes a question of where the best place to validate is.

  2. U got a little error in your calendar generation routine
    line 109
    while($day 1) && ($day 1) && ($day

  3. Great one! Just needed something like this! Thanks for sharing!

    One thing is that february is misspelled in the array! febuary vs february! ;)

    And maybe change the variable being passed as number of month instead of month name…. also to overcome ‘misspellments’….

  4. Plus:
    if(($first_day_in_month == $day_list[$i] || $day > 1) && ($day 1) && ($day

  5. Post went wrong?

    But the at the last if-statement is needs to be
    $day is smaller OR EQUAL to $days_in_month!

  6. Hi Andy

    I’m glad the calendar function is coming in useful for people. Yes you can re-use it – I’m aware that there are a few bugs etc. but I haven’t had a chance to update it yet (apologies)

    I intended it to use the MIT license like Cake itself – when I re factored and cleaned up the code a bit.

    So anyway feel free to use it but I would really appreciate a reference to the source in the code and if you have blog or website just a quick link back to fliplflops.org – but up to you. Somebody else contacted me about using the coed as the basis for a Cake Helper, but I don’t know if they have released any code yet – but it might be worth looking in the bakery.

    Cheers

    John

  7. Hi Evert

    I know there are a few bugs – and thanks for posting details here and as mentioned above in my previous comment I do intend to refactor and then publish this as a proper helper when I get a moment.

    In the meantime though it looks like some of you are finding it useful which is great.

    John

  8. Hello, that’s very nice. Could you please explain how you call the calendar function in a cakephp view ? Thanks again.

  9. $base_url – the url to send the back / foward links on to i.e. the address of page (the calendar expects to be in a mod re-written situation e.g. http://www.flipflops.org/calendar/2008/june)

    What do you mean by that?

    Can’t get that mod re-written to work. When I click next -> my browser opens some dirs that doesnt exist. What do I have to change? Thanks :)

  10. Hi Peter

    The alternative to using it in a mod-rewrite type situation is just putting the year / month in the querystring e.g.

    /calendar.php?year=2008&month=june

    To go the querystring route you will need to edit line 89 of the function above that generates the back and forward links to output a link + querystring rather than just mod rewrite psuedo directory stuff.

    To use something like /calendar/2008/june you will probably need to add a .htaccess file if you don’t have one the rule would need to be something like:

    RewriteEngine On

    RewriteBase /

    RewriteRule ^calendar/([0-9]+)/(*.)$ calendar.php?year=$1&month=$2 [L]

    (haven’t checked the above though – but you get the idea…)

    p.s. there might still be a bug in the above code that won’t show the last day of the month (can’t remember if I have updated it yet).

    p.s. 2 – worth looking at the new version of this at http://www.flipflops.org/2008/04/08/cakephp-calendar-helper/

  11. Every month appears with a day less that it should. You should change the while condition.

    Thanks for the code, by the way!

Comments are closed.