WeberDev.com PHP and MySQL Code

LOG IN
BEGINNER GUIDES  |  PHP CLASSES  |  CODE SEARCH  |  ARTICLES SEARCH  |  PHP FORUMS  |  PHP MANUAL  |  PHP FUNCTIONS LIST  |  WEB SITE TEMPLATES
Start typing to search for PHP and MySQL Code Snippets and Articles Search
Submit a code Example / Snippet Submit Your Code
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
WeberDev's Monthly code contest PHP Code Contest
Your Personal Examples List My Favorite Examples
Your Personal Articles List My Favorite Articles
Edit Account Info Update Your Profile
PHP Code Search
Web Development Forums
Learn MySQL Playing Trivia
PHPBB2 Templates
Web Development Index
PHP Web Logs (BLogs)
Web Development Resources
Web Development Content
PHPClasses
PHP Editor
PHP Jobs
Vision.To Design
Ajax Tutorials
PHP Programming Help
PHP/MySQL Programming
Webmaster Resources
Webmaster Forum
XML meta language
website builder
Submit Site
Forex Trading Online forex trading platform

Go Back Add a Comment Send this example to a friend Add this Article to your personal favoritest for easy future access to your favorite Code Examples and Articles. Submit a code example Print this code example.
BACK ADD A COMMENT SEND TO A FRIEND ADD TO MY FAVORITES ADD CODE EXAMPLES PRINT
Title : Calendar
Categories : PHP, Date Time, Calendar Update Picture
Eric Geerson
Date : Jan 11th 2004
Grade : 3 of 5 (graded 23 times)
Viewed : 13158
File : No file for this code example.
Images : No Images for this code example.
Search : More code by Eric Geerson
Action : Grade This Code Example
Tools : My Examples List

  Submit your own code examples 
 

<?php

//See the calendar working at : LINK1http://geerson.net/gsn/calendar.phpLINK1

function isLeapYear($year){
   
// 1 januari 1900 is monday, use century-check only when 1 januari 1900 is monday;
   
if(($year % 100 == 0) and ($year % 400 != 0)) return(0);
    if(
$year % 4 == 0) return(1);
    return(
0);
}

function
getWeekDay($year, $month, $day){
   
$dcm = array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
   
// 1 januari 1900 is sunday;
   
$gwdYear = 1899;
   
$gwdMonth = 12;
   
$gwdDay = 31;
   
// 1 januari 1900 is monday;
   
$gwdYear = 1899;
   
$gwdMonth = 12;
   
$gwdDay = 30;

   
$diffDays = 0;
   
$diffDays = $diffDays + $day;
    for(
$m = (($year * 12) + $month) - 1; $m > (($gwdYear * 12) + $gwdMonth); $m--)    {
       
$y = (($m - 1) / 12);
       
$i = ($m % 12) + 12;
        if(
isLeapYear($y) == 1)         {
           
$dcm[1] = 29; $dcm[13] = 29;
        }else{
           
$dcm[1] = 28; $dcm[13] = 28;
        }
       
$diffDays = $diffDays + $dcm[$i-1];
    }
   
$diffDays = $diffDays + ($dcm[$gwdMonth - 1] - $gwdDay);
   
$weekDay = ($diffDays % 7) - 2;
    if(
$weekDay < 0) $weekDay = $weekDay + 7;
    return(
$weekDay + 1);
}

function
getDateNumber($year, $month, $day){
   
$diffDays = 0;
    for(
$y = 0; $y < $year; $y++)    {
       
$diffDays = $diffDays + 365;
        if(
isLeapYear($y) == 1) $diffDays = $diffDays + 1;
    }
    for(
$m = 1; $m < $month; $m++)    {
        if(
$m == 1 or $m == 3 or $m == 5 or $m == 7 or $m == 8 or $m == 10 or $m == 12)    {
           
$diffDays = $diffDays + 31;
        }else{
           
$diffDays = $diffDays + 30;
        }
        if(
isLeapYear($year) == 1 and $m == 2) $diffDays = $diffDays - 1;
        if(
isLeapYear($year) == 0 and $m == 2) $diffDays = $diffDays - 2;
    }
   
$diffDays = $diffDays + $day;
    return(
$diffDays);
}

function
CalculateIsoWeekday($year, $month, $day){

// This function converts the weekday-numbers from the
// standard function to an offset acc. to the ISO version
// monday -> 0, ... , sunday -> 6
$n = getWeekday($year, $month, $day);
    return(
$n - 1);
}

function
getWeekNumber($year, $month, $day){
   
// The year value is preset with that of the input date
   
$YearInQuestion = $year;
   
   
// Calculate offset to monday from the input date
   
$InputDateOffset = CalculateIsoWeekday($year, $month, $day);
   
   
// Calculate offsets for the first/last day of the year
   
$January1Offset = CalculateIsoWeekday($YearInQuestion, 1, 1);
   
$December31Offset = CalculateIsoWeekday($YearInQuestion, 12, 31);
   
   
// If the input date is before the 4th of January and the year starts with
    // a friday, saturday or sunday, the week belongs to the previous year
    // if the entered date is not a monday or tuesday
   
if($month == 1 and $day < 4 and $January1Offset > 3 and $InputDateOffset > 1)    {
       
$YearInQuestion = $YearInQuestion - 1;
    }
   
// If the input date is after the 28th of December and the year ends with
    // a monday, tuesday or wednesday, then the week belongs to the following year
    // if the entered date is not a saturday or sunday
   
if ($month == 12 and $day > 28 and $December31Offset < 3 and $InputDateOffset < 5)    {
       
$YearInQuestion = $YearInQuestion + 1;
    }
   
// The 4th of January defines week #1
   
$January4 = getDatenumber($YearInQuestion, 1, 4);
   
   
// Offset to the monday of week #1
   
$FirstMondayOfYear = getDatenumber($YearInQuestion, 1, 4 - CalculateIsoWeekday($YearInQuestion, 1, 4));
   
   
// The time range between the monday of week #1 and the monday
    // of the week in question is divided by 7, plus 1 for the first week
   
$weeknum = (getDateNumber($year, $month, $day) - $InputDateOffset - $FirstMondayOfYear ) / 7 + 1;
    return(
$weeknum);   
}

function
phpdate($year, $month, $day, $format){
    if(
$format == "mmmm")    {
        if(
$month == 1) return("january");
        if(
$month == 2) return("february");
        if(
$month == 3) return("march");
        if(
$month == 4) return("april");
        if(
$month == 5) return("may");
        if(
$month == 6) return("june");
        if(
$month == 7) return("july");
        if(
$month == 8) return("august");
        if(
$month == 9) return("september");
        if(
$month == 10) return("october");
        if(
$month == 11) return("november");
        if(
$month == 12) return("december");
    }
    if(
$format == "yyyy")    {
        return(
$year);
    }
}

function
getCalendar() {
    if(
$_GET["year"] == "") $year = date("Y"); else $year = $_GET["year"];
    if(
$_GET["month"] == "") $month = date("n"); else $month = $_GET["month"];
    if(
$_GET["day"] == "") $day = date("d"); else $day = $_GET["day"];
   
$dow = array("mon", "tue", "wed", "thu", "fri", "sat", "sun");
    if(
isLeapYear($year) == 1)    {
       
$dom = array(31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
    }else{
       
$dom = array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
    }
   
$calendar = "<center><table style='background : ThreedFace; border-style : outset; font-family : Verdana; font-size : 10pt;border-width : 2px;' >";
   
$calendar .= "<tr>";
   
$calendar .= "<th bgcolor=#000080 colspan='8'>";
   
$calendar .= "<font color=white>C A L E N D A R</font>";
   
$calendar .= "</th>";
   
$calendar .= "</tr>";
   
$calendar .= "<tr>";
   
$calendar .= "<th><center>";
   
$m = $month;
   
$y = $year - 1;
    if(
$year != 1900) $calendar .= "<a style='text-decoration : none; color : #808080;' href='?year=" . $y . "&month=" . $m . "&day=" . 1 . "' ><font face=webdings>7</font></a>";
    if(
$month == 1) {
       
$m = 12;
       
$y = $year - 1;
    }else{
       
$m = $month - 1;
       
$y = $year;
    }
    if(!(
$year == 1900 and $month == 1)) $calendar .= "<a style='text-decoration : none; color : #808080;' href='?year=" . $y . "&month=" . $m . "&day=" . 1 . "' ><font face=webdings>3</font></a>";
   
$calendar .= "</th>";
   
$calendar .= "<th nowrap colspan='6'><center>";
   
$calendar .= phpdate($year, $month, $day, "mmmm", $lang) . " " . phpdate($year, $month, $day, "yyyy", $lang);
   
$calendar .= "</th>";
   
$calendar .= "<th><center>";
    if(
$month == 12){
       
$m = 1;
       
$y = $year + 1;
    }else{
       
$m = $month + 1;
       
$y = $year;
    }
   
$calendar .= "<a style='text-decoration : none; color : #808080;' href='?year=" . $y . "&month=" . $m . "&day=" . 1 . "' ><font face=webdings>4</font></a>";
   
$m = $month;
   
$y = $year + 1;
   
$calendar .= "<a style='text-decoration : none; color : #808080;' href='?year=" . $y . "&month=" . $m . "&day=" . 1 . "' ><font face=webdings>8</font></a>";
   
$calendar .= "</th>";
   
$calendar .= "</tr>";
   
$dayno = 2 - getWeekDay($year, $month, 1);
   
$daymin = 1;
   
$daymax = $dom[$month - 1];
    if(
$month == 1) {$daymaxl = 12;} else {$daymaxl = $month - 1;}
    for(
$row = 1; $row <= 7; $row++)    {
        for(
$col = 1; $col <= 8; $col++)        {
           
$calendar .= "<td style='border-color : black; border-width : 2px;' ><center>";
           
// print weekday headers
           
if($row == 1 and $col != 1) {
               
$calendar .= "<font color=#000080>";
               
$calendar .= $dow[$col-1-1];
               
$calendar .= "</font>";
            }
           
// print weeknumbers
           
if($row != 1 and $col == 1)    {   
               
$calendar .= "<font color=#008000>";
                if(
$dayno < $daymin){
                   
$y = $year;
                    if(
$month == 1) $m = 12;
                    if(
$month != 1) $m = $month - 1;
                    if(
$month == 1) $y = $year - 1;
                   
$d = ($dom[$daymaxl - 1]) + $dayno;
                   
$calendar .= sprintf("%02d",getWeekNumber($y, $m, $d));
                }else{
                    if(
$dayno > $daymax){
                       
$y = $year;
                        if(
$month == 12) $m = 1;
                        if(
$month != 12) $m = $month + 1;
                        if(
$month == 12) $y = $year + 1;
                       
$d = $dayno - $daymax;
                       
$calendar .= sprintf("%02d",getWeekNumber($y, $m, $d));
                    }else{
                       
$calendar .= sprintf("%02d",getWeekNumber($year, $month, $dayno));
                    }
                }
               
$calendar .= "</font>";
            }
           
// print day numbers
           
if($row > 1 and $col != 1){
                if(
$dayno < $daymin){
                   
$calendar .= "<font color=#808080>";
                   
$calendar .= ($dom[$daymaxl - 1]) + $dayno;
                   
$calendar .= "</font>";
                }else{
                    if(
$dayno > $daymax){
                       
$calendar .= "<font color=#808080>";
                       
$calendar .= $dayno - $daymax;
                       
$calendar .= "</font>";
                    }else{
                       
$calendar .= $dayno;
                    }
                }
               
$dayno = $dayno + 1;
            }
           
$calendar .= "</td>";
        }
       
$calendar .= "</tr><tr>";
    }
   
$calendar .= "</tr>";
   
$calendar .= "</table>";
    return(
$calendar);
}

?>
<html>
<title>getCalendar()</title>
<body bgcolor=#D0D0D0>
<?php echo(getCalendar()); ?>
</body>
</html>



pcCalendar class - Allows for the creation of calendars in HTML pages. All output functions can be easily overridden, refer to article 1471 for an example.
Categories : PHP, Date Time, Calendar, PHP Classes
A simple and fast calendar combining PHP and tables. Use this as a base for applications in which a calendar is needed.
Categories : Date Time, PHP, Complete Programs, Calendar
Example of using the pcCalendar class, article 1468 on weberdev.com. Calendar example.
Categories : PHP, Date Time, PHP Classes, Calendar
A PHP Calendar function with CSS : add a cool calendar to any php page by just adding a calendar class based function.
Categories : PHP, PHP Classes, Calendar, Date Time
Functions used to define a schedule of holidays. Can define non-fixed holidays (eg. 3rd sunday of June).
Categories : Calendar, Date Time, PHP
Monthly and Daily Upcoming Events calendar.
Categories : Date Time, PostgreSQL, PHP, Calendar, Databases
CALENDAR - easy calendar-navigation with PHP
Categories : PHP, Date Time, HTML and PHP, Calendar
Calendars to choose a range of dates , reservation events ...
Categories : PHP, Calendar, Java Script, Date Time
This script contains 2 functions: 1 to create html select object based on your own customer date format entry- "M d Y h:i.... etc". The second function processes the select object on submit back to unix time.
Categories : PHP, Calendar, Date Time, HTML and PHP
Shows the current time as a PNG-image. This script does not use the GD library. You can use it as a benchmark (because it's slow), or as a quick reference for implementing a simple PNG-file generator.
Categories : Graphics, Zlib, Calendar, PHP, Date Time
Simple PHP control CSS Calender
Categories : PHP, HTML and PHP, Calendar, Date Time, CSS
enhanced date picker with jcript checking for a dynamic date input
Categories : PHP, Java Script, Date Time, Calendar, Arrays
Calendar, Kalender, date, time, day, month, year
Categories : PHP, Date Time, Calendar
Calendar class on the same page , suitable for reservation
Categories : PHP, Calendar, Date Time
function to generate calendars on the fly.
Categories : Calendar, PHP, Date Time
 feha www.vision.to wrote :1115
Modified a bit:
&lt;?php 

error_reporting(`0`); 
ini_set(`display_errors`, `0`); 

//See the calendar working at : http://geerson.nl 
function isLeapYear($year){ 
    // 1 januari 1900 is monday, use century-check only when 1 januari 1900 is monday; 
    if(($year % 100 == 0) and ($year % 400 != 0)) return(0); 
    if($year % 4 == 0) return(1); 
    return(0); 


function getWeekDay($year, $month, $day){ 
    $dcm = array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31); 
    // 1 januari 1900 is sunday; 
    $gwdYear = 1899; 
    $gwdMonth = 12; 
    $gwdDay = 31; 
    // 1 januari 1900 is monday; 
    $gwdYear = 1899; 
    $gwdMonth = 12; 
    $gwdDay = 30; 

    $diffDays = 0; 
    $diffDays = $diffDays + $day; 
    for($m = (($year * 12) + $month) - 1; $m &gt; (($gwdYear * 12) + $gwdMonth); $m--)    { 
        $y = (($m - 1) / 12); 
        $i = ($m % 12) + 12; 
        if(isLeapYear($y) == 1)         { 
            $dcm[1] = 29; $dcm[13] = 29; 
        }else{ 
            $dcm[1] = 28; $dcm[13] = 28; 
        } 
        $diffDays = $diffDays + $dcm[$i-1]; 
    } 
    $diffDays = $diffDays + ($dcm[$gwdMonth - 1] - $gwdDay); 
    $weekDay = ($diffDays % 7) - 2; 
    if($weekDay &lt; 0) $weekDay = $weekDay + 7; 
    return($weekDay + 1); 


function getDateNumber($year, $month, $day){ 
    $diffDays = 0; 
    for($y = 0; $y &lt; $year; $y++)    { 
        $diffDays = $diffDays + 365; 
        if(isLeapYear($y) == 1) $diffDays = $diffDays + 1; 
    } 
    for($m = 1; $m &lt; $month; $m++)    { 
        if($m == 1 or $m == 3 or $m == 5 or $m == 7 or $m == 8 or $m == 10 or $m == 12)    { 
            $diffDays = $diffDays + 31; 
        }else{ 
            $diffDays = $diffDays + 30; 
        } 
        if(isLeapYear($year) == 1 and $m == 2) $diffDays = $diffDays - 1; 
        if(isLeapYear($year) == 0 and $m == 2) $diffDays = $diffDays - 2; 
    } 
    $diffDays = $diffDays + $day; 
    return($diffDays); 


function CalculateIsoWeekday($year, $month, $day){ 

// This function converts the weekday-numbers from the 
// standard function to an offset acc. to the ISO version 
// monday -&gt; 0, ... , sunday -&gt; 6 
$n = getWeekday($year, $month, $day); 
    return($n - 1); 


function getWeekNumber($year, $month, $day){ 

    // The year value is preset with that of the input date 
    $YearInQuestion = $year; 
     
    // Calculate offset to monday from the input date 
    $InputDateOffset = CalculateIsoWeekday($year, $month, $day); 
     
    // Calculate offsets for the first/last day of the year 
    $January1Offset = CalculateIsoWeekday($YearInQuestion, 1, 1); 
    $December31Offset = CalculateIsoWeekday($YearInQuestion, 12, 31); 
     
    // If the input date is before the 4th of January and the year starts with 
    // a friday, saturday or sunday, the week belongs to the previous year 
    // if the entered date is not a monday or tuesday 
    if($month == 1 and $day &lt; 4 and $January1Offset &gt; 3 and $InputDateOffset &gt; 1)    { 
        $YearInQuestion = $YearInQuestion - 1; 
    } 
    // If the input date is after the 28th of December and the year ends with 
    // a monday, tuesday or wednesday, then the week belongs to the following year 
    // if the entered date is not a saturday or sunday 
    if ($month == 12 and $day &gt; 28 and $December31Offset &lt; 3 and $InputDateOffset &lt; 5)    { 
        $YearInQuestion = $YearInQuestion + 1; 
    } 
    // The 4th of January defines week #1 
    $January4 = getDatenumber($YearInQuestion, 1, 4); 
     
    // Offset to the monday of week #1 
    $FirstMondayOfYear = getDatenumber($YearInQuestion, 1, 4 - CalculateIsoWeekday($YearInQuestion, 1, 4)); 
     
    // The time range between the monday of week #1 and the monday 
    // of the week in question is divided by 7, plus 1 for the first week 
    $weeknum = (getDateNumber($year, $month, $day) - $InputDateOffset - $FirstMondayOfYear ) / 7 + 1; 
    return($weeknum);     


function phpdate($year, $month, $day, $format){ 

    if($format == "mmmm")    { 
        if($month == 1) return("January"); 
        if($month == 2) return("Jebruary"); 
        if($month == 3) return("March"); 
        if($month == 4) return("April"); 
        if($month == 5) return("May"); 
        if($month == 6) return("June"); 
        if($month == 7) return("July"); 
        if($month == 8) return("August"); 
        if($month == 9) return("September"); 
        if($month == 10) return("October"); 
        if($month == 11) return("November"); 
        if($month == 12) return("December"); 
    } 
    if($format == "yyyy")    { 
        return($year); 
    } 


function getCalendar() {

    if($_GET["year"] == "") $year = date("Y"); else $year = $_GET["year"]; 
    if($_GET["month"] == "") $month = date("n"); else $month = $_GET["month"]; 
    if($_GET["day"] == "") $day = date("d"); else $day = $_GET["day"]; 
    $dow = array("Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"); 
    if(isLeapYear($year) == 1)    { 
        $dom = array(31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31); 
    }else{ 
        $dom = array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31); 
    } 
    $calendar = "&lt;table style=`border-style : outset; font-family : Verdana, Geneva, sans-serif;font-size : 8pt;border-width : 1px;` &gt;"; 
    //$calendar .= "&lt;tr&gt;"; 
    //$calendar .= "&lt;th bgcolor=#d5d5d5 colspan=`8`&gt;"; 
    //$calendar .= "&lt;font color=white&gt;C A L E N D A R&lt;/font&gt;"; 
    //$calendar .= "&lt;/th&gt;"; 
    //$calendar .= "&lt;/tr&gt;"; 
    $calendar .= "&lt;tr&gt;"; 
    $calendar .= "&lt;th&gt;&lt;center&gt;"; 
    $m = $month; 
    $y = $year - 1; 
    if($year != 1900) $calendar .= "&lt;a style=`text-decoration : none; color : #808080;` href=`?year=" . $y . "&month=" . $m . "&day=" . 1 . "` &gt;&lt;font face=\"webdings\"&gt;7&lt;/font&gt;&lt;/a&gt;"; 
    if($month == 1) { 
        $m = 12; 
        $y = $year - 1; 
    }else{ 
        $m = $month - 1; 
        $y = $year; 
    } 
    if(!($year == 1900 and $month == 1)) $calendar .= "&lt;a style=`text-decoration : none; color : #808080;` href=`?year=" . $y . "&month=" . $m . "&day=" . 1 . "` &gt;&lt;font face=\"webdings\"&gt;3&lt;/font&gt;&lt;/a&gt;"; 
    $calendar .= "&lt;/th&gt;"; 
    $calendar .= "&lt;th nowrap colspan=`6`&gt;&lt;center&gt;"; 
    $calendar .= phpdate($year, $month, $day, "mmmm", $lang) . " " . phpdate($year, $month, $day, "yyyy", $lang); 
    $calendar .= "&lt;/th&gt;"; 
    $calendar .= "&lt;th&gt;&lt;center&gt;"; 
    if($month == 12){ 
        $m = 1; 
        $y = $year + 1; 
    }else{ 
        $m = $month + 1; 
        $y = $year; 
    } 
    $calendar .= "&lt;a style=`text-decoration : none; color : #808080;` href=`?year=" . $y . "&month=" . $m . "&day=" . 1 . "` &gt;&lt;font face=\"webdings\"&gt;4&lt;/font&gt;&lt;/a&gt;"; 
    $m = $month; 
    $y = $year + 1; 
    $calendar .= "&lt;a style=`text-decoration : none; color : #808080;` href=`?year=" . $y . "&month=" . $m . "&day=" . 1 . "` &gt;&lt;font face=\"webdings\"&gt;8&lt;/font&gt;&lt;/a&gt;"; 
    $calendar .= "&lt;/th&gt;"; 
    $calendar .= "&lt;/tr&gt;"; 
    $dayno = 2 - getWeekDay($year, $month, 1); 
    $daymin = 1; 
    $daymax = $dom[$month - 1]; 
    if($month == 1) {$daymaxl = 12;} else {$daymaxl = $month - 1;} 
    for($row = 1; $row &lt;= 7; $row++)    { 
        for($col = 1; $col &lt;= 8; $col++)        { 
            $calendar .= "&lt;td style=`border-style : outset; font-family : Verdana, Geneva, sans-serif;font-size : 8pt;border-width : 1px;` &gt;&lt;center&gt;"; 
            // print weekday headers 
            if($row == 1 and $col != 1) { 
                $calendar .= "&lt;font color=\"#000080\"&gt;";
                $calendar .= $dow[$col-1-1]; 
                $calendar .= "&lt;/font&gt;"; 
            } 
            // print weeknumbers 
            if($row != 1 and $col == 1)    {     
                //$calendar .= "&lt;font color=#008000&gt;"; 
                if($dayno &lt; $daymin){ 
                    $y = $year; 
                    if($month == 1) $m = 12; 
                    if($month != 1) $m = $month - 1; 
                    if($month == 1) $y = $year - 1; 
                    $d = ($dom[$daymaxl - 1]) + $dayno;
           $current_week = date("W");
           $week_numbers = sprintf("%02d",getWeekNumber($y, $m, $d));
           if($current_week == $week_numbers)