PHP Dates and Times Using Non-Gregorian Calendars - Web Development and Design | Tutorial for Java, PHP, HTML, Javascript PHP Dates and Times Using Non-Gregorian Calendars - Web Development and Design | Tutorial for Java, PHP, HTML, Javascript

Breaking

Post Top Ad

Post Top Ad

Monday, May 13, 2019

PHP Dates and Times Using Non-Gregorian Calendars

PHP Dates and Times




Using Non-Gregorian Calendars

Problem

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


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   Converting between Julian days and the Gregorian calendar

            // 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.

Discussion

Example   Using the Julian calendar

           // 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.


Example   Using the French Republican calendar

           // 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.



No comments:

Post a Comment

Post Top Ad