Thursday, 25th January 2007CSS PHP Calendar

A PHP Dynamic Calendar that produces a Valid CSS & XHTML Calendar

As per my previous article for the ASP version of the calendar script (here), the PHP script below produces a valid XHTML & CSS based calendar.  I have created an example of the ASP version of this script being used a Date Picker for a form, see the article for that here.

The Code 

You can copy and paste the script from below download it as a zip here:  php css calendar.zip

 

 


$day = @$_GET["day"];
$month = @$_GET["month"];
$year = @$_GET["year"];

if ($month == ""){
$day = date('d');
$month = date('n');
$year = date('Y');
}
$firstOfMonthDateStamp = mktime(12, 59, 59, $month, 1, $year);

$daysInCurrentMonth = date("t", $firstOfMonthDateStamp);
$firstOfMonthDay = date("d", $firstOfMonthDateStamp);
$lastOfMonthDay = date("w", strtotime(date( "n",$firstOfMonthDateStamp )."/".$daysInCurrentMonth."/".date("Y",$firstOfMonthDateStamp)));
$previousMonth = getdate(strtotime('-1 month', $firstOfMonthDateStamp));
$nextMonth = getdate(strtotime('+1 month', $firstOfMonthDateStamp));
?>




//ASP/PHP WRITTEN BY BOB MCKAY, WWW.FRESHMANGO.COM - USE AS YOU WISH BUT PLEASE LEAVE THIS IN PLACE
//DESIGN, CSS AND XHTML ALL BY VEERLE, HTTP://VEERLE.DUOH.COM
$columnCounter = 0;
//FOR fillLeadingEmptyDays = 1 TO ($firstOfMonthDay - 1)
for ($fillLeadingEmptyDays=0; $fillLeadingEmptyDays <= ($firstOfMonthDay - 1); $fillLeadingEmptyDays+=1) {
$columnCounter = $columnCounter + 1;
echo " ";
}
for ($enterDays=1; $enterDays <= $daysInCurrentMonth; $enterDays+=1) {
$columnCounter = $columnCounter + 1;
echo " ";
if ( $columnCounter == 7 ) {
echo " ";
$columnCounter = 0;
}
}
for ($fillEmptyDays=($columnCounter + 1); $fillEmptyDays <= 7; $fillEmptyDays+=1) {
if ($columnCounter ==0) {
break;
}
echo " ";

}
?>


<< >>
SMTWTFS
 
if ($enterDays == $day) { echo " class = \"today\""; }
$dayLinkDestination = "#";
echo " href=\"".$dayLinkDestination."\">".$enterDays."
 

Comment On This Article

Article Comments

  • 6

    mike, 07:10
    29 January 2008

    Hi,
    this looks highly useful script. Any chances to demonstrate how to use it in PHP? My server does not cope with ASP :(

  • 5

    Bob, 10:18
    16 May 2007

    Hi William,

    Its fairly easy to implment the calendar as a "date picker" for a form - I've actually done this for the ASP version of the script here: http://www.freshmango.com/articles/display/?articleID=18.

    You should quite easily be able to use the Javascript from ASP version to adapt the PHP version (all it requires is making each date a link which calls the javascript function, passing a variable to it at the same time.

    At some point I'll put together a PHP version but I swamped at the moment.

    All the best

    Bob

  • 4

    William, 04:54
    15 May 2007

    I like the interface - but how do I use it to populate a form - this would require each "date" being an input of some sort -
    Wills

  • 3

    Belal El-Essawy, 10:35
    08 April 2007

    Is it possible to add events to this calendar? Thanks

  • 2

    Bob, 18:54
    28 March 2007

    Oneasmany,
    Thanks for the post - I'll check over the CSS link although in all honesty I expected everyone to take the CSS for the calendar and paste it in to their own CSS file. I hope the alternative CSS files we're useful too!
    If you have any other comments I'd love to hear them - I'm an ASP developer and so my PHP skills are very limited (this is my first and only script so far).

  • 1

    Oneasmany, 13:50
    28 March 2007

    File calendar.php calls calendar.css file only if "/inc" is deleted (it doesn't work not even creating folder "inc", I don't know why).
    Thank you :).