InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

Menu
  • Home
  • Sitemap

Python Programming Language Best Tutorials and Code Examples

Learn Python Right Now!
Home
PHP
Using Non-Gregorian Calendars in PHP
PHP

Using Non-Gregorian Calendars in PHP

InfinityCoder November 18, 2016

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

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-26 converts between Julian days and the familiar Gregorian calendar.

Example 3-26. Converting between Julian days and the Gregorian calendar

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// March 8, 1876
// $jd is 2406323, the Julian day count
$jd = gregoriantojd(3,9,1876);
 
$gregorian = cal_from_jd($jd, CAL_GREGORIAN);
/* $gregorian is array('date' => '3/9/1876',
                       'month' => 3,
                       'day' => 9,
                       'year' => 1876,
                       'dow' => 4,
                       'abbrevdayname' => 'Thu',
                       'dayname' => 'Thursday',
                       'abbrevmonth' => 'Mar',
                       'monthname' => 'March'));
*/

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

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

Example 3-27. Using the Julian calendar

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
// February 29, 1900 (not a Gregorian leap year)
// $jd is 2415092, the Julian day count
$jd = cal_to_jd(CAL_JULIAN, 2, 29, 1900);
 
$julian = cal_from_jd($jd, CAL_JULIAN);
/* $julian is array('date' => '2/29/1900',
                    'month' => 2,
                    'day' => 29,
                    'year' => 1900,
                    'dow' => 2,
                    'abbrevdayname' => 'Tue',
                    'dayname' => 'Tuesday',
                    'abbrevmonth' => 'Feb',
                    'monthname' => 'February'));
*/
$gregorian = cal_from_jd($jd, CAL_GREGORIAN);
/* $gregorian is array('date' => '3/13/1900',
                       'month' => 3,
                       'day' => 13,
                       'year' => 1900,
                       'dow' => 2,
                       'abbrevdayname' => 'Tue',
                       'dayname' => 'Tuesday',
                       'abbrevmonth' => 'Mar',
                       'monthname' => 'March'));
*/

The valid range for the Julian calendar is 4713 BCE to 9999 CE, but because 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-28.

Example 3-28. Using the French Republican calendar

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
// 13 Floréal XI
// $jd is 2379714, the Julian day count
$jd = cal_to_jd(CAL_FRENCH, 8, 13, 11);
 
$french = cal_from_jd($jd, CAL_FRENCH);
/* $french is array('date' => '8/13/11',
                    'month' => 8,
                    'day' => 13,
                    'year' => 11,
                    'dow' => 2,
                    'abbrevdayname' => 'Tue',
                    'dayname' => 'Tuesday',
                    'abbrevmonth' => 'Floreal',
                    'monthname' => 'Floreal'));
*/
 
// May 3, 1803 - sale of Louisiana to the US
$gregorian = cal_from_jd($jd, CAL_GREGORIAN);
/* $gregorian is array('date' => '5/3/1803',
                       'month' => 5,
                       'day' => 3,
                       'year' => 1803,
                       'dow' => 2,
                       'abbrevdayname' => 'Tue',
                       'dayname' => 'Tuesday',
                       'abbrevmonth' => 'May',
                       'monthname' => 'May'));
*/

The valid range for the French Republican calendar is September 1792 to September 1806, which is small, but because 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-29.

Example 3-29. Using the Jewish calendar

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
// 25 Kislev 5774 is the first night/day of Hanukah
// $jd is 2456625, the Julian day count
$jd = cal_to_jd(CAL_JEWISH, 3, 25, 5774);
 
$jewish = cal_from_jd($jd, CAL_JEWISH);
/* $jewish is array('date' => '3/25/5774',
                    'month' => 3,
                    'day' => 25,
                    'year' => 5774,
                    'dow' => 4,
                    'abbrevdayname' => 'Thu',
                    'dayname' => 'Thursday',
                    'abbrevmonth' => 'Kislev',
                    'monthname' => 'Kislev'));
*/
 
// November 28, 2013 is US Thanksgiving holiday
$gregorian = cal_from_jd($jd, CAL_GREGORIAN);
/* $gregorian is array('date' => '11/28/2013',
                       'month' => 11,
                       'day' => 28,
                       'year' => 2013,
                       'dow' => 4,
                       'abbrevdayname' => 'Thu',
                       'dayname' => 'Thursday',
                       'abbrevmonth' => 'Nov',
                       'monthname' => 'November'));
 
*/

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.

Share
Tweet
Email
Prev Article
Next Article

Related Articles

Using Form Elements with Multiple Options in PHP
You have form elements that let a user select multiple …

Using Form Elements with Multiple Options in PHP

Calculating Trigonometric Functions in PHP
You want to use trigonometric functions, such as sine, cosine, …

Calculating Trigonometric Functions in PHP

About The Author

InfinityCoder
InfinityCoder

Leave a Reply

Cancel reply

Recent Tutorials InfinityQuest

  • Adding New Features to bash Using Loadable Built-ins in bash
    Adding New Features to bash Using Loadable …
    June 27, 2017 0
  • Getting to the Bottom of Things in bash
    Getting to the Bottom of Things in …
    June 27, 2017 0

Recent Comments

  • fer on Turning a Dictionary into XML in Python
  • mahesh on Turning a Dictionary into XML in Python

Categories

  • Bash
  • PHP
  • Python
  • Uncategorized

InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

About Us

Start learning your desired programming language with InfinityQuest.com.

On our website you can access any tutorial that you want with video and code examples.

We are very happy and honored that InfinityQuest.com has been listed as a recommended learning website for students.

Popular Tags

binary data python CIDR convert string into datetime python create xml from dict python dictionary into xml python how to create xml with dict in Python how to write binary data in Python IP Address read binary data python tutorial string as date object python string to datetime python

Archives

  • June 2017
  • April 2017
  • February 2017
  • January 2017
  • December 2016
  • November 2016
Copyright © 2021 InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more
Programming Tutorials | Sitemap