Calendar is a set of extensions for date calendar. How to use this Calendar extension in PHP? This article will introduce to you how to install and use the Calendar extension.
Calendar is a set of extensions for the date calendar, but for us, it does not have operations related to the lunar calendar, so for us Chinese, this extension does not matter. actual effect. But this does not prevent us from understanding and learning it.
Date calendar type
For date operations under normal circumstances, PHP uses UTC time by default, which is international standard time , for our country, we need to add 8 to the standard UTC time to represent the East Eighth District of Beijing time. Of course, you can also directly modify the time zone related parameters in php.ini or the database to define the date and time zone.
The standard time zone corresponds to the Gregorian time calendar. That is the Gregorian calendar and solar calendar dates that we most commonly use now. In addition to this standard Gregorian calendar, the Calendar extension also supports the Jewish calendar, the French calendar, and another very famous Julian calendar. Our Gregorian calendar evolved from the Julian calendar. The difference between them is not big, but the gap between the Jewish calendar and the French calendar is relatively large. In the following code, we will see the difference between various calendars. difference.
Regarding the specific content of these calendars, you can check the relevant information by yourself, and you can also learn some interesting historical knowledge, such as why the Julian calendar was abandoned and the Gregorian calendar was changed to the Gregorian calendar. Also, why are the years in the French calendar only short, and why are the years in the Jewish calendar so memorable? The origins of the names of the months in the French calendar and the Jewish calendar are all very interesting stories.
Calendar extension installation and calendar information viewing
The Calendar extension has been integrated into the PHP installation package and does not need to be installed separately. If you cannot use Calendar-related functions, you can recompile PHP and add the --enable-calendar parameter.
Next let’s look at the details of the specified calendar. Here we specify the Jewish calendar.
$info = cal_info(2); print_r($info); // Array // ( // [months] => Array // ( // [1] => Tishri // [2] => Heshvan // [3] => Kislev // [4] => Tevet // [5] => Shevat // [6] => Adar I // [7] => Adar II // [8] => Nisan // [9] => Iyyar // [10] => Sivan // [11] => Tammuz // [12] => Av // [13] => Elul // ) // [abbrevmonths] => Array // ( // [1] => Tishri // [2] => Heshvan // [3] => Kislev // [4] => Tevet // [5] => Shevat // [6] => Adar I // [7] => Adar II // [8] => Nisan // [9] => Iyyar // [10] => Sivan // [11] => Tammuz // [12] => Av // [13] => Elul // ) // [maxdaysinmonth] => 30 // [calname] => Jewish // [calsymbol] => CAL_JEWISH // )
cal_info() The parameter received by the function is a constant, which are CAL_GREGORIAN, CAL_JULIAN, CAL_JEWISH, and CAL_FRENCH. Their corresponding numbers are 0, 1, 2, and 3. In this code, what we return is the CAL_JEWISH information. It can be seen that the names of the months in the Jewish calendar are different from the English names in the Gregorian calendar. For example, there are Nisan and Tishri. I won’t delve into the specific content. After all, we have no access to this kind of calendar in our daily lives.
You can try the information returned by other calendars yourself. The Gregorian calendar and the Julian calendar are the same, but the names of the months in the Gregorian calendar are different, and these names are very interesting.
Date Calendar Conversion
First, to perform calendar conversion, we need to convert the specified date into a Julian Days count. This jd count can be regarded as an intermediate variable of Calendar expansion, used for conversion between various calendars.
// 转变Unix时间戳为Julian Day计数 $today = unixtojd(mktime(0, 0, 0, 9, 23, 2020)); echo $today, PHP_EOL; // 2459116
Use the unixtojd() function to convert a unix timestamp into a jd count. Next, let’s take a look at which day in the Jewish calendar corresponds to September 23, 2020.
// 获取当前犹太历时间 print_r(cal_from_jd($today, CAL_JEWISH)); // Array // ( // [date] => 1/5/5781 // [month] => 1 // [day] => 5 // [year] => 5781 // [dow] => 3 // [abbrevdayname] => Wed // [dayname] => Wednesday // [abbrevmonth] => Tishri // [monthname] => Tishri // )
The returned information is already very clear. Our day is Wednesday, January 5, 5781 in the Jewish calendar. The current month is Tishri, which corresponds to the first month of the Gregorian calendar year and the seventh month of the church year in the Jewish calendar, which represents the arrival of autumn.
cal_from_jd() function is to return detailed information of the specified calendar based on jd count. The other cal_to_jd() function converts a supported calendar data to a jd count.
echo cal_to_jd(CAL_JEWISH, 1, 5, 5781), PHP_EOL; // 2459116 echo cal_to_jd(CAL_GREGORIAN,9, 23, 2020), PHP_EOL; // 2459116
You can see that the jd count returned by the Jewish calendar above is consistent with the jd count returned by our Gregorian calendar.
Of course, we can also convert the jd count date into unix time.
echo date("Y-m-d", jdtounix($today)), PHP_EOL; // 2020-09-23
In addition to cal_from_jd() and cal_to_jd(), the Calendar extension also provides us with some quick functions for date conversion, but they return string type date information directly, not like The cal_from_jd() function also returns date details.
// 转变一个Gregorian历法日期到Julian Day计数 $jd = GregorianToJD(9, 23, 2020); // 转变一个Julian Day计数为Gregorian历法日期 echo jdtogregorian($jd), PHP_EOL; // 9/23/2020 // 转变一个Julian Day计数为Julian历法日期 echo jdtojulian($jd), PHP_EOL; // 9/10/2020 // 转变一个Julian Day计数为犹太历法日期 echo jdtojewish($jd), PHP_EOL; // 1/5/5781 // 转变一个Julian Day计数为unix时间戳 echo jdtounix($jd), PHP_EOL; // 1600819200 $jd = GregorianToJD(9, 23, 1799); // 转变一个Julian Day计数为French历法日期 echo jdtofrench($jd), PHP_EOL; // 1/1/8
GregorianToJD() method is to quickly convert a Gregorian calendar date to jd count. The functions of jdtoxxxx quickly return the string information of the date calendar corresponding to the jd count.
Please note that the calendar date can only be the date within the period from September 22, 1792 to September 22, 1806, which is the calendar introduced after the founding of the First French Republic. , and ended in use in 1806, as Napoleon founded the First French Empire in 1804. The empire abolished the French calendar (Republican calendar) and fully implemented the Gregorian calendar.
The number of days in a certain month
How did you learn the above historical knowledge? Next, let’s return to learning about the Calendar extension.
$num = cal_days_in_month(CAL_GREGORIAN, 2, 2020); echo $num, PHP_EOL; // 29
cal_days_in_month() 函数是返回指定历法月份的天数,比如我们看看 2020 年的 2月 是不是 闰月 就可以用这个函数来实现。
复活节彩蛋
复活节是西方非常重要的一个节日,所以在 Calendar 扩展中就有函数可以直接获得指定年份的复活节日期。关于复活节的计算方式其实还是比较复杂的,手工推算是比较麻烦的,而程序计算就非常方便了。
// 指定年份的复活节时间戳 echo date("M-d-Y", easter_date(2019)), PHP_EOL; // Apr-21-2019 echo date("M-d-Y", easter_date(2020)), PHP_EOL; // Apr-12-2020 echo date("M-d-Y", easter_date(2021)), PHP_EOL; // Apr-04-2021 // 3月21日到复活节之间的天数 echo easter_days(2019), PHP_EOL; // 31 echo easter_days(2020), PHP_EOL; // 22 echo easter_days(2021), PHP_EOL; // 14
easter_date() 函数就是返回指定年份的复活节日期。而 easter_days() 函数则是返回从当年公历的 3月21日 到复活节之间的天数。
复活节是每年春分月圆后的第一个星期日,而春分一般是在3月21日,这就简化为只要计算满月的日期和紧挨的星期日的日期就可以得到每年复活节的具体日期了。这种函数在西方世界的软件开发中会非常常用,其实就像我们需要获取每年春节的具体公历日期一样。
总结
是不是很有意思的一套扩展函数。不过对于我们主要面向国内开发的开发者来说用处确实不大,但笔者在学习这个扩展的时候另外收获了不少历史方面的知识,也算是开了眼界,也不失为一大收获,大家也自己试着玩玩并且查查相关的历史知识吧,说不定你的收获会更多!
测试代码:
https://github.com/zhangyue0503/dev-blog/blob/master/php/202009/source/10.PHP中非常好玩的Calendar扩展学习.php
参考文档:https://www.php.net/manual/zh/book.calendar.php
推荐学习:《PHP视频教程》
The above is the detailed content of What is Calendar extension in PHP? how to use?. For more information, please follow other related articles on the PHP Chinese website!