Dead Simple PHP Calendar

I needed a calendar for a PHP project I was working on. I did a quick look around and found some promising solutions, but they all seemed way more complex than what I wanted. They also used tables, which are gross. This is a ridiculously easy way to get a calendar from php:

function calendar($date=false){
  $date_parts=(!$date)?preg_split("/[-]+/",$date):preg_split("/[-]+/",date('Y-m-d'));
  $year=$date_parts[0];$month=$date_parts[1];$day=$date_parts[2];
  $time=mktime(0,0,0,$month,1,$year);
  $month_name=date('F',$time);
  $days_in_month=cal_days_in_month(CAL_GREGORIAN,$month,$year);
  $first_day=date('w',$time);
  $calendar='<div id="calendar"><h6 id="1169_month_name-year_1" >'.$month_name.' '.$year.'</h6><ol id="cal_body"><li>Su</li><li>Mo</li><li>Tu</li><li>We</li><li>Th</li><li>Fr</li><li>Sa</li>';
  for($x=0;$x< $first_day;++$x)$calendar.='<li>&nbsp';
  for($x=1;$x< =$days_in_month;++$x)$calendar.=($x==$day)?'<li class="highlight">'.$x.'':'<li>'.$x.'</li>';
  return $calendar.'</ol></div>';
}

How it works
It is actually pretty simple. If you feed the function a date it will make a calendar and highlight that day of the month. If you do not feed the calendar a date it will make a calendar and highlight today’s date.

How to use it
The simplest way to call this function is <?php echo calendar(); ?> or if you are feeling romantic you can specify a date <?php echo calendar('2012-2-14'); ?> You must format the date YYYY-MM-DD (leading zeros are optional).

Making it look nice
Tables are not the worse thing in the world, but they are not even close to the best way to do this. Styling the calendar in CSS is a far better choice, especially if you want to go above and beyond with you calendar. If you just want a basic calendar you can use CSS like this:

#calendar{position:absolute;top:6em;left:50em;width:14em;min-height:12.5em;margin:0 auto;text-align:center;font-size:0.6em}
#calendar h6{font-size:1em;margin:0 1em;display:inline}
#cal_body li{list-style:none;width:1em;height:0.8em;float:left;padding:0.2em;margin:0.1em;text-align:center}
.highlight{color:#E81490}

Of course we can make this more complex
For example, we could add navigation to get to the next month or previous month. We could also make the dates into links. For my purposes I did both.

Adding monthly navigation can be done by editing one line:$calendar='<div id="calendar"><a href="index.php?date='.date('Y-m',strtotime("last Month",$time)).'-1" class="cal_nav">< <</a><h6 id="1169_month_name-year_2" >'.$month_name.' '.$year.'</h6></a><a href="index.php?date='.date('Y-m',strtotime("next Month",$time)).'-1" class="cal_nav">>></a><ol id="cal_body"><li>Su</li><li>Mo</li><li>Tu</li><li>We</li><li>Th</li><li>Fr</li><li>Sa</li>'</ol></div>If you want to use that, you will probably need to edit the link so that it connects to whatever/wherever your calendar is located.

Making the dates into links is also just a simple matter of editing one (long) line:for($x=1;$x< =$days_in_month;++$x)$calendar.=($x==$day)?'<li class="highlight">'.$x.'':'<li><a href="index.php?date='.$year.'-'.$month.'-'.$x.'">'.$x.'</a></li>';Again, you would need to edit that link so that it connects to everything to the function.

Share and Enjoy:
  • Print
  • Digg
  • StumbleUpon
  • del.icio.us
  • Facebook
  • Twitter
  • Google Bookmarks
  • RSS
  • Slashdot

Tags: , , , ,

Leave a Reply