TinyButStrong - the PHP Template Engine
Categories > Your tips & tricks >

TinyButStrong Simple Calendar

The forum is closed. Please use Stack Overflow for submitting new questions. Use tags: tinybutstrong , opentbs

By: Rudy
Date: 2010-05-01
Time: 12:48

TinyButStrong Simple Calendar

Hello,

here's an example for a really simple calendar using TBS I wanted to share. The code is commented and should be self explaining. No database is needed to run the script.

calendar.php:
<?php
error_reporting(E_ALL | E_STRICT);

require 'tbs_class_php5.php';

$tbs = new clsTinyButStrong;

/* ---------------- change to your needs ------------------- */

date_default_timezone_set('Europe/Rome'); //see: http://www.php.net/manual/en/timezones.php
list($startyear, $startmonth) = array(date('Y'), date('m')); //start from current year and month
//list($startyear, $startmonth) = array(2011, 1);
$monthcount = 14; //number of months to show

/* use own localization strings because setlocale() is not thread safe */
$monthsofyear = array('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December');
$weekdays = array('Mo','Tu','We','Th','Fr','Sa','Su');

/* function to add your own css class to single day cells depending on the date
* this can be used to highlight certain days for example
*/
function dayclass($year, $month, $day) {
  $class = array();
  $date = mktime(0,0,0,$month,$day,$year); //unix timestamp for current day to use with date()
  if(date('N', $date) > 5) { //saturday and sunday
    $class[] = 'we'; //add class 'we' for 'weekend' to cell
  }
  /* do your stuff here, add classes to $class
   * example: */
  if (date('d', $date) == 1) {
    $class[] = 'fi'; //add class 'fi' for first in month
  }
  return implode(' ', $class); //return concatenated classes
}

/* --------------------------------------------- */

$day = array('y'=>null,'m'=>null,'d'=>null,'class'=>null); //a day consists of the date parts and a class, extend to your needs
$week = array_fill(0, 7, $day); //fill a week with 7 days
$month = array_fill(0, 6, $week); //fill a month with 6 weeks
$calendar = array_fill(0, $monthcount, $month); //fill calendar with $monthcount months
$months = array();
foreach($calendar as $c =>& $month) {
  list($m, $y) = array(($startmonth+$c-1) % 12 + 1, $startyear+floor(($startmonth+$c-1)/12)); //calculate current month and year
  $months[$c] = array('name'=>$monthsofyear[$m-1], 'year'=>$y); //name of month and year to merge with header of months
  $firstday = mktime(0,0,0,$m,1,$y);  //start from first in month
  $firstweekday = date('N', $firstday)-1; //determine weekday of first day (starting from Monday)
  $daysinmonth = date('t', $firstday); //how many days has the month
  foreach($month as $w => & $week) {
    foreach($week as $d => & $day) {
      $d = $d+1 - $firstweekday + $w * 7; //calculate current day in month
      if ($d > 0 && $d <= $daysinmonth) { //only print valid values
        $day = array_merge($day, array('y'=>$y, 'm'=>$m, 'd'=>$d));
        $day['class'] = dayclass($y, $m, $d); //add custom classes to cells
      }
    }
  }
}
$tbs->loadTemplate('calendar.html');
$tbs->mergeblock('weekdays', $weekdays); //merge Mo-Su
$tbs->mergeblock('months', $months); //merge all months with headers
$tbs->mergeblock('weeks', range(0, 5)); //merge 6 weeks in every month = rows
$tbs->mergeblock('days', range(0, 6)); //merge 7 days in each week of every month = columns
$tbs->mergefield('calendar', $calendar); //finally merge the calculated values into the empty calendar
$tbs->show();

?>

calendar.html:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  <head>
    <title>TinyButStrong Simple Calendar Example</title>
    <meta http-equiv="content-type" content="text/html; charset=utf-8" />
    <meta name="content-language" content="en" />
    <style type="text/css">
      /* <!-- */
      * { margin:0; padding:0; font:12px Arial, Helvetica, sans-serif; }
      body { padding: 15px; }
      table.calendar { width: 200px; float:left; margin:0 10px 10px 0;}
      table.calendar caption { background:#FBE182; border-style:solid solid solid none; color:black; font-weight:bold; border: 1px solid #FAA61A; }
      table.calendar caption span { display:block; border:1px solid white; padding:3px 0; background:#f7c40a;  }
      table.calendar { border-collapse: collapse; padding:2px; float:left; }
      table.calendar thead th { padding:2px 6px; color:#666; text-align:center; }
      table.calendar tbody td { border:1px solid white;  height:20px; background:#efefef; color:#999; text-align: center; padding:2px 4px; font-size:12px;  }
      table.calendar tbody tr.even td { background:#fff2c1; }

      table.calendar tbody td.we { color:red; } /* weekend */
      table.calendar tbody td.fi { background:white; color:blue; } /* weekend */
      /* --> */
    </style>
  </head>
  <body>
    <table class="calendar">[months;block=table]
      <caption><span>[months.name] [months.year]</span></caption>
      <thead><tr><th>[weekdays.val;block=th;p1]</th></tr></thead>
      <tbody>
        <tr><td>[calendar.[months.$].[weeks.$].[days.$].class;att=class;magnet=#;noerr]
            [calendar.[months.$].[weeks.$;block=tr;p1].[days.$;block=td;p1].d;noerr]</td></tr>
      </tbody>
    </table>
  </body>
</html>

Hope this helps someone.

Bye
Rudy
By: Skrol29
Date: 2010-05-01
Time: 23:48

Re: TinyButStrong Simple Calendar

thank you very much, I'll try it
By: TomH
Date: 2010-05-22
Time: 13:22

Re: TinyButStrong Simple Calendar

Very nice Rudy,

Thanks for contributing to the TBS code base.

FYI, there is some TBS code for using a database and for admin of adding/editing entries here http://tomhenry.us/tbs3/