LiveZilla Live Help

Opal Creations Blog | A bit about what we do and what we know!

Feb/10

24

Free PHP Linear Year View Calendar

So by now I’m sure you have been searching a long time to find a script or some code to help you build a SIMPLE php calendar script which displays the whole year to a page. Well hopefully this post will help you out. This script will help you build a yearly linear calendar, we needed this to view all our our clients renewal dates in a clear manner. My example code is is self contained in one file (you can split into modular files if you wish). There is no javascript needed, it is all in the PHP and HTML/CSS. Requires PHP4+

This code can easily be modded for database integration (which is what we are to use it for). We will try to help if anyone has any issues. At the moment you can pass in a YEAR parameter (either GET OR POST) and it will render that year… for example: http://www.opal-creations.co.uk/blog/demos/php-linear-calendar/?year=2022

This is only the skin and bones of designing the PHP yearly calendar so please style it and add any relevant javascript/database connections as required.

First of all the screenshot:

Now here is the DEMO URL:
http://www.opal-creations.co.uk/blog/demos/php-linear-calendar/

Here is the code:

<!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">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Linear Calendar</title>
<style type="text/css" media="all">
body {
font-family:"Calibri";
color:#009;
}
.currentDay {
background:#FFC;
color:red;
}
table {
border-collapse:collapse;
border:1px #009 solid;
}
td {
height:60px;
vertical-align:top;
text-align:center;
width:30px;
}
.days:hover {
background:#9F0;
border-color:#000;
}
.day6 {
background:#ECECFF;
}
.day7 {
background:#ECECFF;
}
.monthName {
text-align:left;
vertical-align:middle;
}
.monthName div {
padding-left:10px;
}
</style>
</head>

<body>
<?php
$dDaysOnPage = 37;
$dDay = 1;
if ($_REQUEST['year'] <> "") { $dYear = $_REQUEST['year']; } else { $dYear = date("Y"); }
?>
<table width="100%" border="1" cellspacing="0" cellpadding="0">
<tr>
<th><?php echo $dYear; ?></th>
<th>Mo</th>
<th>Tu</th>
<th>We</th>
<th>Th</th>
<th>Fr</th>
<th>Sa</th>
<th>Su</th>
<th>Mo</th>
<th>Tu</th>
<th>We</th>
<th>Th</th>
<th>Fr</th>
<th>Sa</th>
<th>Su</th>
<th>Mo</th>
<th>Tu</th>
<th>We</th>
<th>Th</th>
<th>Fr</th>
<th>Sa</th>
<th>Su</th>
<th>Mo</th>
<th>Tu</th>
<th>We</th>
<th>Th</th>
<th>Fr</th>
<th>Sa</th>
<th>Su</th>
<th>Mo</th>
<th>Tu</th>
<th>We</th>
<th>Th</th>
<th>Fr</th>
<th>Sa</th>
<th>Su</th>
<th>Mo</th>
<th>Tu</th>
</tr>
<?php
function FriendlyDayOfWeek($dayNum) {
// converts the sunday to 7
// This function can be removed in php 5 by - date("N"),
// just remove function calls below and replace by swapping date("w" for date("N"
if ($dayNum == 0){ return 7; } else { return $dayNum; }
}

for ($mC=1;$mC<=12;$mC++) {
$currentDT = mktime(0,0,0,$mC,$dDay,$dYear);
echo "<tr><td class='monthName'><div>".date("F",$currentDT)."</div></td>";
$daysInMonth = date("t",$currentDT);

echo InsertBlankTd(FriendlyDayOfWeek(date("w",$currentDT))-1);

for ($i=1;$i<=$daysInMonth;$i++) {
$exactDT = mktime(0,0,0,$mC,$i,$dYear);
if ($i==date("d")&&date("m",$currentDT)==date("m")) { $class="currentDay"; } else { $class = ""; }
echo "<td class='".$class." days day".FriendlyDayOfWeek(date("w",$exactDT))."'>".$i."</td>";
}

echo InsertBlankTd($dDaysOnPage - $daysInMonth - FriendlyDayOfWeek(date("w",$currentDT))+1);
echo "</tr>";
}
?>
</table>
</body>
</html>
<?php
function InsertBlankTd($numberOfTdsToAdd) {
for($i=1;$i<=$numberOfTdsToAdd;$i++) {
$tdString .= "<td></td>";
}
return $tdString;
}
?>

· · ·

8 comments

  • Mohamed Chaara · January 30, 2013 at 5:24 pm

    I love this calendar! well done!

    I have tried to replicate it though, but it did not come out the way it comes in your website.

    When I look at the code in the article, line 123: $tdString .= “”;
    is the period intentional? it returns an error whenever I run it..
    When I remove it, the code seems to work, except all the months start on Monday, and the code breaks when a month actually starts on a Monday.

    I’ll play around with the code, maybe I can fix it, if not, I will sure ask for help.

    and great website. Keep up the good work!

    Reply

    • Mohamed Chaara · January 30, 2013 at 6:53 pm

      I modified the code a bit, and got it right. The issue was in the InsertBlankTd function. I placed the function at the top of the code, and modified it as follows:

      function InsertBlankTd($numberOfTdsToAdd) {

      for($i=1;$i<=$numberOfTdsToAdd-1;$i++) {
      echo "”;
      }
      }

      Thanks for the code. It’s great!

      Reply

      • Admin comment by admin · January 30, 2013 at 6:58 pm

        Ah thanks for pointing it out.

        For the record, the period is intentional, it is a shorthand way of concatenating strings. For example:

        $a = “HELLO”;

        $a = $a . “WORLD;
        //is the same as
        $a.= “WORLD”;

        Reply

        • Mohamed Chaara · January 30, 2013 at 8:02 pm

          Thanks! It’s just that for some reason it wasn’t working on my server, and I knew it must be working because it is fine on the demo.
          I placed what I did to get it to work here because I thought it might help someone someday.

          Thanks again for the code. it’s a great job, and a great find.

          Reply

    • Jerahmeel · September 17, 2013 at 3:58 am

      can be the first day will be Sunday?

      Reply

  • Darren Bellinger · April 17, 2012 at 4:42 pm

    I really like your calendar and I am new to coding with PHP… I was wondering if you knew of a way to display alternate weekends, from a given date, in a different colour?

    Regards

    Darren

    Reply

    • Andrew Robins · April 17, 2012 at 4:54 pm

      Hi Darren,

      You could generate a php array of all weekend dates and then iterate through and remove all even array indexes (or odd).

      Reply

  • Duarte Briz · July 26, 2010 at 3:16 pm

    Very nice script!

    I have been looking for something like this for a long time.

    I´m trying to mod this for an intranet so it can show the staff vacations. It will be the staff name on the y axis and the day of the month on the x axis, and it should show only one month at the time.

    You could have commented that part a little bit more.

    Regards,

    DB

    Reply

Leave a Reply

<<

>>

Theme Design by devolux.nh2.me