To demonstrate Objects in PHP I put together a calendar include.
The calendar was created in a PHP file to use as an include. The code was as follows:
<?php class DisplayCal { public $day; public $month; public $year; public $timestamp; private $prevYearNav; private $nextYearNav; private $prevYear; private $nextYear; private $prevMonth; private $nextMonth; private $dayOfMonth; private $fullMonth; private $noMonth; private $firstDayTimeStamp; private $dateArray; private $firstDay; private $lengthMonth; private $totalSlots; private $endSlots; private $addToEnd; private $gridLength; private $displayHTML; public function __construct($d = 0, $m = 0, $y = 0){ if($d == 0){ $this->day = date("j"); }else{ $this->day = $d; } if($m == 0){ $this->month = date("n"); }else{ $this->month = $m; } if($y == 0){ $this->year = date("Y"); }else{ $this->year = $y; } ########## build values ############# $this->prevYearNav= $this->year -1; $this->nextYearNav= $this->year + 1; $this->prevYear= $this->year; $this->nextYear= $this->year; $this->prevMonth= $this->month-1; $this->nextMonth= $this->month+1; if ($this->prevMonth == 0 ) { $this->prevMonth= 12; $this->prevYear = $this->year - 1; } if ($this->nextMonth == 13 ) { $this->nextMonth = 1; $this->nextYear = $this->year + 1; } $this->timestamp = mktime(0,0,0,$this->month,$this->day,$this->year); $this->dayOfMonth = date('j',$this->timestamp); $this->fullMonth = date('F',$this->timestamp); $this->noMonth = date('n',$this->timestamp); $this->firstDayTimeStamp = mktime(0,0,0,$this->noMonth,1,$this->year); $this->dateArray = getdate($this->firstDayTimeStamp ); $this->firstDay = $this->dateArray['wday']; if($this->firstDay == 0){ $this->firstDay = 6; }else{ $this->firstDay--; } $this->lengthMonth = date('t',$this->timestamp); $this->totalSlots = $this->firstDay + $this->lengthMonth; $this->totalSlots--; $this->endSlots = $this->totalSlots % 7; $this->addToEnd = 7 - $this->endSlots; $this->gridLength = $this->totalSlots + $this->addToEnd; } public function displayCal(){ $this->displayHTML = "<table>"; $this->displayHTML .= "<tr><td colspan=\"7\" class=\"center\">{$this->fullMonth} - {$this->year}</td></tr>"; $this->displayHTML .= "<tr class=\"mainCal\"><td>M</td><td>T</td><td>W</td><td>T</td><td>F</td><td>S</td><td>S</td></tr>"; for ($i=0; $i<$this->gridLength; $i++) { if(($i % 7) == 0 ){ $this->displayHTML .= "<tr>"; } if($i < $this->firstDay || ($i) > ($this->totalSlots)){ $this->displayHTML .= "<td>-</td>"; }else{ if(($i - $this->firstDay + 1) == $this->day){ $this->displayHTML .= "<td><span class=\"pickedDay\">". ($i - $this->firstDay + 1) . "</span></td>"; }else{ if(($i % 7) == 6){ $this->displayHTML .= "<td class=\"sun\">". ($i - $this->firstDay + 1) . "</td>" ; }else{ $this->displayHTML .= "<td>". ($i - $this->firstDay + 1) . "</td>"; } } } if(($i % 7) == 6 ){ $this->displayHTML .= "</tr>"; } } $this->displayHTML .= "<tr class=\"mainCal\">"; $this->displayHTML .= "<td><a href=\"{$_SERVER['PHP_SELF']}?month={$this->month}&year={$this->prevYearNav}\"><</a></td>"; $this->displayHTML .= "<td colspan=\"5\">Years</td>"; $this->displayHTML .= "<td><a href=\"{$_SERVER['PHP_SELF']}?month={$this->month}&year={$this->nextYearNav}\">></a></td>"; $this->displayHTML .= "</tr>"; $this->displayHTML .= "<tr class=\"mainCal\">"; $this->displayHTML .= "<td><a href=\"{$_SERVER['PHP_SELF']}?month={$this->prevMonth}&year={$this->prevYear}\"><</a></td>"; $this->displayHTML .= "<td colspan=\"5\">Months</td>"; $this->displayHTML .= "<td><a href=\"{$_SERVER['PHP_SELF']}?month={$this->nextMonth}&year={$this->nextYear}\">></a></td>"; $this->displayHTML .= "</tr></table>"; return $this->displayHTML; } } ?>
This was then included on the page with:
<?php include('table-cal.php'); if(!isset($_GET["day"])){ $_GET["day"] = date("j"); } if(!isset($_GET["month"])){ $_GET["month"] = date("n"); } if(!isset($_GET["year"])){ $_GET["year"] = date("Y"); } $cDay = $_GET["day"]; $cMonth = $_GET["month"]; $cYear = $_GET["year"]; $obj = new DisplayCal($cDay, $cMonth, $cYear); echo $obj->displayCal(); ?>
And the resultant table was styled up with the following CSS:
table{ font-family: Helvetica, arial, sans-serif; font-size: 13px; border-collapse: collapse; border: 1px solid #C24704; box-shadow: 4px 4px 4px #555; } th, td{ padding:5px; } tr:nth-of-type(odd){ background-color:#AD6C49; color:#fff; } .center{ text-align:center; } tr.mainCal td{ width:30px; text-align:center; } table a:link{ text-decoration:none; color:#333; } table a:hover{ color:#6C3; } .pickedDay{ border:1px solid #f00; display:inline-block; padding:5px; } td.sun{ color:#ff0000; }
Now how many days is it until Christmas?
Awesome php artist, Nice work
Great guide.
Do you know if it is possible to make all Sundays marked with red?
Many thanks in advanced.
Yes, it is. I have changed the class slightly and added a new style to colour the Sunday’s red.
Hi,
good job ! :].
but i found 1 mistake : function displayCal() should be named as displayHTML.