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.
// 轉(zhuǎn)變Unix時間戳為Julian Day計數(shù) $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.
// 獲取當(dāng)前猶太歷時間 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.
// 轉(zhuǎn)變一個Gregorian歷法日期到Julian Day計數(shù) $jd = GregorianToJD(9, 23, 2020); // 轉(zhuǎn)變一個Julian Day計數(shù)為Gregorian歷法日期 echo jdtogregorian($jd), PHP_EOL; // 9/23/2020 // 轉(zhuǎn)變一個Julian Day計數(shù)為Julian歷法日期 echo jdtojulian($jd), PHP_EOL; // 9/10/2020 // 轉(zhuǎn)變一個Julian Day計數(shù)為猶太歷法日期 echo jdtojewish($jd), PHP_EOL; // 1/5/5781 // 轉(zhuǎn)變一個Julian Day計數(shù)為unix時間戳 echo jdtounix($jd), PHP_EOL; // 1600819200 $jd = GregorianToJD(9, 23, 1799); // 轉(zhuǎn)變一個Julian Day計數(shù)為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() 函數(shù)是返回指定歷法月份的天數(shù),比如我們看看 2020 年的 2月 是不是 閏月 就可以用這個函數(shù)來實現(xiàn)。
復(fù)活節(jié)彩蛋
復(fù)活節(jié)是西方非常重要的一個節(jié)日,所以在 Calendar 擴展中就有函數(shù)可以直接獲得指定年份的復(fù)活節(jié)日期。關(guān)于復(fù)活節(jié)的計算方式其實還是比較復(fù)雜的,手工推算是比較麻煩的,而程序計算就非常方便了。
// 指定年份的復(fù)活節(jié)時間戳 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日到復(fù)活節(jié)之間的天數(shù) echo easter_days(2019), PHP_EOL; // 31 echo easter_days(2020), PHP_EOL; // 22 echo easter_days(2021), PHP_EOL; // 14
easter_date() 函數(shù)就是返回指定年份的復(fù)活節(jié)日期。而 easter_days() 函數(shù)則是返回從當(dāng)年公歷的 3月21日 到復(fù)活節(jié)之間的天數(shù)。
復(fù)活節(jié)是每年春分月圓后的第一個星期日,而春分一般是在3月21日,這就簡化為只要計算滿月的日期和緊挨的星期日的日期就可以得到每年復(fù)活節(jié)的具體日期了。這種函數(shù)在西方世界的軟件開發(fā)中會非常常用,其實就像我們需要獲取每年春節(jié)的具體公歷日期一樣。
總結(jié)
是不是很有意思的一套擴展函數(shù)。不過對于我們主要面向國內(nèi)開發(fā)的開發(fā)者來說用處確實不大,但筆者在學(xué)習(xí)這個擴展的時候另外收獲了不少歷史方面的知識,也算是開了眼界,也不失為一大收獲,大家也自己試著玩玩并且查查相關(guān)的歷史知識吧,說不定你的收獲會更多!
測試代碼:
https://github.com/zhangyue0503/dev-blog/blob/master/php/202009/source/10.PHP中非常好玩的Calendar擴展學(xué)習(xí).php
參考文檔:https://www.php.net/manual/zh/book.calendar.php
推薦學(xué)習(xí):《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!

Hot AI Tools

Undress AI Tool
Undress images for free

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

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

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

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

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

In PHP, you can use square brackets or curly braces to obtain string specific index characters, but square brackets are recommended; the index starts from 0, and the access outside the range returns a null value and cannot be assigned a value; mb_substr is required to handle multi-byte characters. For example: $str="hello";echo$str[0]; output h; and Chinese characters such as mb_substr($str,1,1) need to obtain the correct result; in actual applications, the length of the string should be checked before looping, dynamic strings need to be verified for validity, and multilingual projects recommend using multi-byte security functions uniformly.

AgeneratorinPHPisamemory-efficientwaytoiterateoverlargedatasetsbyyieldingvaluesoneatatimeinsteadofreturningthemallatonce.1.Generatorsusetheyieldkeywordtoproducevaluesondemand,reducingmemoryusage.2.Theyareusefulforhandlingbigloops,readinglargefiles,or

To prevent session hijacking in PHP, the following measures need to be taken: 1. Use HTTPS to encrypt the transmission and set session.cookie_secure=1 in php.ini; 2. Set the security cookie attributes, including httponly, secure and samesite; 3. Call session_regenerate_id(true) when the user logs in or permissions change to change to change the SessionID; 4. Limit the Session life cycle, reasonably configure gc_maxlifetime and record the user's activity time; 5. Prohibit exposing the SessionID to the URL, and set session.use_only

The urlencode() function is used to encode strings into URL-safe formats, where non-alphanumeric characters (except -, _, and .) are replaced with a percent sign followed by a two-digit hexadecimal number. For example, spaces are converted to signs, exclamation marks are converted to!, and Chinese characters are converted to their UTF-8 encoding form. When using, only the parameter values ??should be encoded, not the entire URL, to avoid damaging the URL structure. For other parts of the URL, such as path segments, the rawurlencode() function should be used, which converts the space to . When processing array parameters, you can use http_build_query() to automatically encode, or manually call urlencode() on each value to ensure safe transfer of data. just

You can use substr() or mb_substr() to get the first N characters in PHP. The specific steps are as follows: 1. Use substr($string,0,N) to intercept the first N characters, which is suitable for ASCII characters and is simple and efficient; 2. When processing multi-byte characters (such as Chinese), mb_substr($string,0,N,'UTF-8'), and ensure that mbstring extension is enabled; 3. If the string contains HTML or whitespace characters, you should first use strip_tags() to remove the tags and trim() to clean the spaces, and then intercept them to ensure the results are clean.

There are two main ways to get the last N characters of a string in PHP: 1. Use the substr() function to intercept through the negative starting position, which is suitable for single-byte characters; 2. Use the mb_substr() function to support multilingual and UTF-8 encoding to avoid truncating non-English characters; 3. Optionally determine whether the string length is sufficient to handle boundary situations; 4. It is not recommended to use strrev() substr() combination method because it is not safe and inefficient for multi-byte characters.

To set and get session variables in PHP, you must first always call session_start() at the top of the script to start the session. 1. When setting session variables, use $_SESSION hyperglobal array to assign values ??to specific keys, such as $_SESSION['username']='john_doe'; it can store strings, numbers, arrays and even objects, but avoid storing too much data to avoid affecting performance. 2. When obtaining session variables, you need to call session_start() first, and then access the $_SESSION array through the key, such as echo$_SESSION['username']; it is recommended to use isset() to check whether the variable exists to avoid errors

Key methods to prevent SQL injection in PHP include: 1. Use preprocessing statements (such as PDO or MySQLi) to separate SQL code and data; 2. Turn off simulated preprocessing mode to ensure true preprocessing; 3. Filter and verify user input, such as using is_numeric() and filter_var(); 4. Avoid directly splicing SQL strings and use parameter binding instead; 5. Turn off error display in the production environment and record error logs. These measures comprehensively prevent the risk of SQL injection from mechanisms and details.
