# Recipe 3.15. Using Non-Gregorian Calendars

#### 3.15.1. Problem

You want to use a non-Gregorian calendar, such as a Julian, Jewish, or French Republican calendar.

#### 3.15.2. Solution

PHP's calendar extension provides conversion functions for working with the Julian calendar, as well as the French Republican and Jewish calendars. To use these functions, the calendar extension must be loaded.

These functions use the Julian day count (which is different than the Julian calendar) as their intermediate format to move information between them. cal_to_jd( ) converts a month, day, and year to a Julian day count value; cal_from_jd( ) converts a Julian day count value to a month, day, and year in a particular calendar. Example 3-41 converts between Julian days and the familiar Gregorian calendar.

##### Converting between Julian days and the Gregorian calendar

 ` string(8) "3/9/1876" ["month"]=> int(3) ["day"]=> int(9) ["year"]=> int(1876) ["dow"]=> int(4) ["abbrevdayname"]=> string(3) "Thu" ["dayname"]=> string(8) "Thursday" ["abbrevmonth"]=> string(3) "Mar" ["monthname"]=> string(5) "March" } */ ?>`

The valid range for the Gregorian calendar is 4714 BCE to 9999 CE.

#### 3.15.3. Discussion

To convert between Julian days and the Julian calendar, use the CAL_JULIAN constant, as shown in Example 3-42.

##### Using the Julian calendar

 ` string(9) "2/29/1900" ["month"]=> int(2) ["day"]=> int(29) ["year"]=> int(1900) ["dow"]=> int(2) ["abbrevdayname"]=> string(3) "Tue" ["dayname"]=> string(7) "Tuesday" ["abbrevmonth"]=> string(3) "Feb" ["monthname"]=> string(8) "February" } */ \$gregorian = cal_from_jd(\$jd, CAL_GREGORIAN); /* \$gregorian is an array: array(9) { ["date"]=> string(9) "3/13/1900" ["month"]=> int(3) ["day"]=> int(13) ["year"]=> int(1900) ["dow"]=> int(2) ["abbrevdayname"]=> string(3) "Tue" ["dayname"]=> string(7) "Tuesday" ["abbrevmonth"]=> string(3) "Mar" ["monthname"]=> string(5) "March" } */ ?>`

The valid range for the Julian calendar is 4713 BCE to 9999 CE, but since it was created in 46 BCE, you run the risk of annoying Julian calendar purists if you use it for dates before that.

To convert between Julian days and the French Republican calendar, use the CAL_FRENCH constant , as shown in Example 3-43.

##### Using the French Republican calendar

 ` string(7) "8/13/11" ["month"]=> int(8) ["day"]=> int(13) ["year"]=> int(11) ["dow"]=> int(2) ["abbrevdayname"]=> string(3) "Tue" ["dayname"]=> string(7) "Tuesday" ["abbrevmonth"]=> string(7) "Floreal" ["monthname"]=> string(7) "Floreal" } */ // May 3, 1803 - sale of Louisiana to the US \$gregorian = cal_from_jd(\$jd, CAL_GREGORIAN); /* \$gregorian is an array: array(9) { ["date"]=> string(8) "5/3/1803" ["month"]=> int(5) ["day"]=> int(3) ["year"]=> int(1803) ["dow"]=> int(2) ["abbrevdayname"]=> string(3) "Tue" ["dayname"]=> string(7) "Tuesday" ["abbrevmonth"]=> string(3) "May" ["monthname"]=> string(3) "May" } */ ?>`

The valid range for the French Republican calendar is September 1792 to September 1806, which is small, but since the calendar was only in use from October 1793 to January 1806, that's comprehensive enough. Note that the month names that cal_from_jd( ) returns do not have proper accents'they are, for example, Floreal instead of Floréal.

To convert between Julian days and the Jewish calendar, use the CAL_JEWISH constant , as shown in Example 3-47.

##### Using the Jewish calendar

 ` string(9) "6/14/5761" ["month"]=> int(6) ["day"]=> int(14) ["year"]=> int(5761) ["dow"]=> int(5) ["abbrevdayname"]=> string(3) "Fri" ["dayname"]=> string(6) "Friday" ["abbrevmonth"]=> string(5) "AdarI" ["monthname"]=> string(5) "AdarI" } */ \$gregorian = cal_from_jd(\$jd, CAL_GREGORIAN); /* \$gregorian is an array: array(9) { ["date"]=> string(8) "3/9/2001" ["month"]=> int(3) ["day"]=> int(9) ["year"]=> int(2001) ["dow"]=> int(5) ["abbrevdayname"]=> string(3) "Fri" ["dayname"]=> string(6) "Friday" ["abbrevmonth"]=> string(3) "Mar" ["monthname"]=> string(5) "March" } */ ?>`

The valid range for the Jewish calendar starts with 3761 BCE (year 1 on the Jewish calendar). Note that whether or not it falls within a leap year, the month Adar is always returned as AdarI. In leap years, Adar II is returned as AdarII.

Documentation for the calendar functions at http://www.php.net/calendar; the history of the Gregorian calendar is explained at http://scienceworld.wolfram.com/astronomy/GregorianCalendar.html .

PHP Cookbook: Solutions and Examples for PHP Programmers
ISBN: 0596101015
EAN: 2147483647
Year: 2006
Pages: 445