code, technology, tips

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='

'.$month_name.' '.$year.'
  1. Su
  2. Mo
  3. Tu
  4. We
  5. Th
  6. Fr
  7. Sa
  8. ';
    for($x=0;$x< $first_day;++$x)$calendar.='

  9.  ';
    for($x=1;$x< =$days_in_month;++$x)$calendar.=($x==$day)?'
  10. '.$x.'':'

  11. '.$x.'
  12. ';
    return $calendar.'

';
}

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 or if you are feeling romantic you can specify a date 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='

< <

'.$month_name.' '.$year.'

>>

  1. Su
  2. Mo
  3. Tu
  4. We
  5. Th
  6. Fr
  7. Sa
  8. '

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)?'

  • '.$x.'':'

  • '.$x.'
  • ';Again, you would need to edit that link so that it connects to everything to the function.

    Standard
    technology, tips, wordpress

    The Perils of Using a Free WordPress Theme

    Take a look at what happened to this blog’s hit counts after switching (briefly) to a new and free WordPress theme downloaded from the official WordPress website.

    Before I used my own variation of the freely available sandbox theme. I switched after updating the template andlosing some of my work on it. Now I’ve gone back with though I haven’t put any real effort into making the site look “good.”

    Since replacing the theme, my site’s stats have returned to normal. I won’t name the theme that killed my blog’s stats for about a week, because that’s mean, but it should serve as a warning. By itself the sandbox theme is fairly SEO friendly, but with with a little tinkering you can get something that knocks the mittens off kittens. Sandbox is the exception though, not the rule. Many of the themes available out there a garbage. It is better to make your own or pay someone with brains to do it for you.

    Remember, free is sometimes good, but not always.

    Standard