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!

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python are both high-level programming languages that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

Atom editor mac version download
The most popular open source editor

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment