PHP
Calendar Introduction
·
The calendar extension contains functions that simplifies
converting between different calendar formats.
·
It is based on the Julian Day
Count, which is a count of days starting from January 1st, 4713 B.C.
·
Note: To convert between calendar formats, you
must first convert to Julian Day Count, then to the calendar of your choice.
Installation
For
these functions to work, you have to compile PHP with --enable-calendar.
The
Windows version of PHP has built-in support for this extension.
Calendar
Functions:
- cal_days_in_month — Return the number of days in a month for a
given year and calendar
- cal_from_jd — Converts from Julian Day Count to a supported
calendar
- cal_info — Returns information about a particular calendar
- cal_to_jd — Converts from a supported calendar to Julian Day Count
- easter_date — Get Unix timestamp for midnight on Easter of a given
year
- easter_days — Get number of days after March 21 on which Easter
falls for a given year
- FrenchToJD — Converts a date from the French Republican Calendar
to a Julian Day Count
- GregorianToJD — Converts a Gregorian date to Julian Day Count
- JDDayOfWeek — Returns the day of the week
- JDMonthName — Returns a month name
- JDToFrench — Converts a Julian Day Count to the French Republican
Calendar
- JDToGregorian — Converts Julian Day Count to Gregorian date
- jdtojewish — Converts a Julian day count to a Jewish calendar date
- JDToJulian — Converts a Julian Day Count to a Julian Calendar Date
- jdtounix — Convert Julian Day to Unix timestamp
- JewishToJD — Converts a date in the Jewish Calendar to Julian Day
Count
- JulianToJD — Converts a Julian Calendar date to Julian Day Count
- unixtojd — Convert Unix timestamp to Julian Day
PHP
code for calendar:
<?php
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>
Output: