Skip to main content
  • Home
  • Tags
OpenView360
Think Smart, Be Free, Choose Open Source
Home » Blogs » CoolGoose's blog

Simple PHP Calendar

In:
  • calendar
  • coding
  • php
12Nov2009

I was searching today for a pre-made script for a simple php calendar and after a bit of search I've found this.

The code is a bit old, and the author's website doesn't seem to work anymore, so I decided to clean it up a bit.
You can see the result below.

  1. <?php
  2. // This gets today's date
  3. $date = time ();
  4.  
  5. // This puts the day, month, and year in seperate variables
  6. $day  = date('d', $date);
  7. $month= date('m', $date);
  8. $year = date('Y', $date);
  9.  
  10. // Here we generate the first day of the month
  11. $first_day = mktime(0,0,0,$month, 1, $year);
  12.  
  13. //This gets us the month name
  14. $title = date('F', $first_day);
  15.  
  16. //Here we find out what day of the week the first day of the month falls on
  17. $day_of_week = date('D', $first_day);
  18.  
  19. //Once we know what day of the week it falls on, we know how many blank days occure before it. If the first day of the week is a Sunday then it would be zero
  20. switch($day_of_week) {
  21.   case "Sun": $blank = 0; break;
  22.   case "Mon": $blank = 1; break;
  23.   case "Tue": $blank = 2; break;
  24.   case "Wed": $blank = 3; break;
  25.   case "Thu": $blank = 4; break;
  26.   case "Fri": $blank = 5; break;
  27.   case "Sat": $blank = 6; break;
  28. }
  29.  
  30. // We then determine how many days are in the current month
  31. $days_in_month = cal_days_in_month(0, $month, $year);
  32. ?>
  33.  
  34. <table cellpadding="0" cellspacing="0">
  35.   <tr>
  36.     <th colspan="7"><?php print $month; ?> <?php print $year; ?></th>
  37.   </tr>
  38.   <tr>
  39.     <td>Sun</td>
  40.     <td>Mon</td>
  41.     <td>Tue</td>
  42.     <td>Wed</td>
  43.     <td>Thu</td>
  44.     <td>Fri</td>
  45.     <td>Sat</td>
  46.   </tr>
  47.    <?php $day_count = 1;// This counts the days in the week, up to 7 ?>
  48.   <tr>
  49.   <?php while ($blank > 0): //first we take care of those blank days ?>
  50.     <td>&nbsp;</td> <?php $blank = $blank-1; $day_count++; ?>
  51.   <?php endwhile; ?>
  52.   <?php $day_num = 1; //sets the first day of the month to 1 ?>  
  53.  
  54.   <?php while ($day_num <= $days_in_month): //count up the days, until we've done all of them in the month ?>
  55.     <td><?php print $day_num; ?></td>
  56.     <?php $day_num++; $day_count++; ?>
  57.     <?php if ($day_count > 7): //Make sure we start a new row every week ?>
  58.   </tr>
  59.   <tr>
  60.     <?php $day_count = 1; ?>
  61.     <?php endif; ?>
  62.   <?php endwhile; ?>
  63.    
  64.   <?php while ($day_count >1 && $day_count <=7): //Finaly we finish out the table with some blank details if needed ?>
  65.     <td>&nbsp;</td>
  66.     <?php $day_count++; ?>
  67.   <?php endwhile; ?>
  68.   </tr>
  69. </table>

  • CoolGoose's blog

Comments

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>. Beside the tag style "<foo>" it is also possible to use "[foo]".

More information about formatting options

Entries (RSS)

About

Just your average goose.

Activity Stream

  • Fri, 12/03/2010 - 18:18

  • Digg CoolGoose dugg Fish Developed With 'Six-Pack Abs' 6:18pm#
  • Digg CoolGoose dugg Tumblr More Than a BILLION Times (Graphic) 6:13pm#
  • Digg CoolGoose dugg China Warns Google - &quot; Bear The Consequences&quot; 6:13pm#
  • Digg CoolGoose dugg Seven alternatives to the Apple iPad 6:13pm#
  • Digg CoolGoose dugg Top 50 Free Browser-Based Games 5:32pm#
  • Digg CoolGoose dugg Beautiful innovation: the first 20 hours of FF XIII 5:32pm#
  • Digg CoolGoose dugg President Obama Discusses Three Strikes Anti-Piracy Law 5:22pm#
  • Digg CoolGoose dugg Motion Control Trailers - Who Does it Best? 4:48pm#

Tags in Tags

boris akunin carti css demon design devils digg erast fandorin gand general gnome iubire joey goebel kde linux love melancolie open source party php plugins poem power user problem protest quest random random thoughts regex regina noptii remorse renastere site tristete ubuntu windows
more tags

Search

Community

belletristisch.com - Online Literature
OS Revolution - Living in a matrix
At-byte.com - Stay Sharp
Linux PHP IDE

Who's online

There are currently 0 users and 0 guests online.

Syndicate

Syndicate content
Copyright alexandrubucur.com

Drupal port by 3rdWorld : Created by Design Disease