PHP provides a large number of built-in functions, which allows developers to handle time with ease and greatly improves work efficiency. Introduces some common PHP date and time functions and date and time processing.
Commonly used date and time processing functions
|
Description |
||||||||||||||||||||||||
checkdate | Verify the time function to determine whether the time is valid, return true if valid, otherwise return false | ||||||||||||||||||||||||
date_default_timezone_get | Get the default time zone used by script datetime functions | ||||||||||||||||||||||||
date_default_timezone_set | Set the default time zone for date and time functions | ||||||||||||||||||||||||
date | Format a local time/date | ||||||||||||||||||||||||
getdate | Get date/time information | ||||||||||||||||||||||||
gettimeofday | Get the current time | ||||||||||||||||||||||||
localtime | Get local time | ||||||||||||||||||||||||
microtime | Returns the current timestamp and microseconds | ||||||||||||||||||||||||
mktime | Get a UNIX timestamp | ||||||||||||||||||||||||
strtotime | Parse any English text datetime description into a UNIX timestamp | ||||||||||||||||||||||||
time | Returns the current UNIX timestamp |
System time zone setting
During the learning process, I found that the time obtained through the date() function is different from the local time. This is due to PHP5 rewriting the date() function. Therefore, the current date and time function is 8 less than the system time. Hour. The default setting in the PHP language is standard Greenwich Time (that is, the zero time zone is used). There are two main ways to change the time zone setting in PHP language:
1. Modify the settings in the php.ini file, find the;date.timezone = option under [date], change this item to date.timezone=Asia/Hong_Kong, and then restart the apache server.
2. In the application, add the following function before using the time and date function:
date_default_timezone_set(“Asia/Hong_Kong”); After the setting is completed, the date() function can be used normally and there will be no time difference problem.
UNIX timestamp
The timestamp is the creation, modification, and access time in the file attributes. Digital time stamp service (DTS) is one of the web website security services that can provide security protection for the date and time information of electronic files.
A timestamp is an encrypted credential document, which consists of 3 parts:
² Files that need to be timestamped are encrypted with Hash encoding to form a digest.
² DTS accepts the date and time information of the file.
² Encrypt received DTS files.
The digital time is added by the certification unit DTS, based on the time when DTS receives the file. The working principle of timestamp is to convert the time value into an encrypted value through other encryption methods. When the time changes, the encrypted value also changes. The advantage of the timestamp is that the changing encryption value prevents the value from being stolen and illegally reused, which also plays the role of encryption. Timestamps mainly rely on time and generate a unique value within an agreed period of time.
mktime() function
Syntax:
int mktime(int hour, int minute, int month, int day, int year, int [is_dst])
Parameter
|
Description |
||||||||||||||||
hour | Hours | ||||||||||||||||
minute | Minutes | ||||||||||||||||
second | Number of seconds (within one minute) | ||||||||||||||||
month | Month number | ||||||||||||||||
day | Days | ||||||||||||||||
year | Number of years | ||||||||||||||||
is_dst | The parameter is_dst can be set to 1 during daylight saving time, or 0 if not; if not sure whether it is daylight saving time, set to -1 (default value) |
date() function
date(string format,int timestamp)
This function will return a string generated by the timestamp parameter according to the specified format. The parameter timestamp is optional. If omitted, the current time is used. The format parameter allows developers to output the time and date in the format they specify.
date_default_timezone_set(PRC); //Set Beijing time.
format character |
Description | Return value example |
---|---|---|
日 | --- | --- |
d | The day of the month, a 2-digit number with leading zeros | 01 to 31 |
D | Day of the week, text representation, 3 letters | Mon to Sun |
j | The day of the month, without leading zeros | 1 to 31 |
l (lower case letter of L) | Day of the week, complete text format | Sunday to Saturday |
N | The day of the week represented by numbers in ISO-8601 format (newly added in PHP 5.1.0) | 1 (meaning Monday) to 7 (meaning Sunday) |
S | The English suffix after the day of the month, 2 characters | st, nd, rd or th. Can be used together with j |
w | The day of the week, represented by numbers | 0 (meaning Sunday) to 6 (meaning Saturday) |
z | Day of the year | 0 to 365 |
Week | --- | --- |
W | The week number of the year in ISO-8601 format, each week starts on Monday (new in PHP 4.1.0) | Example: 42 (the 42nd week of the year) |
Month | --- | --- |
F | Month, complete text format, such as January or March | January to December |
m | The month represented by the number, with leading zeros | 01 to 12 |
M | Three-letter abbreviation for the month | Jan to Dec |
n | Numeric month, without leading zeros | 1 to 12 |
t | The number of days in a given month | 28 to 31 |
Year | --- | --- |
L | Whether it is a leap year | If it is a leap year, it is 1, otherwise it is 0 |
o | ISO-8601 format year number. This is the same value as Y, except that if the ISO week number (W) belongs to the previous or next year, that year is used. (New in PHP 5.1.0) | Examples: 1999 or 2003 |
Y | Full 4-digit year | For example: 1999 or 2003 |
y | 2-digit year | For example: 99 or 03 |
Time | --- | --- |
a | Lowercase AM and PM values | am or pm |
A | Capital AM and PM values | AM or PM |
B | Swatch Internet Standard Time | 000 to 999 |
g | Hour, 12-hour format, no leading zeros | 1 to 12 |
G | Hour, 24 hour format, no leading zeros | 0 to 23 |
h | Hour, 12 hour format, with leading zeros | 01 to 12 |
H | Hour, 24-hour format, with leading zeros | 00 to 23 |
i | Minutes with leading zeros | 00 to 59> |
s | Seconds, with leading zeros | 00 to 59> |
u | Milliseconds (new in PHP 5.2.2). It should be noted that the date() function always returns 000000 because it only accepts integer parameters, and DateTime::format() only supports milliseconds. | Example: 654321 |
Time Zone | --- | --- |
e | Time zone identifier (new in PHP 5.1.0) | For example: UTC, GMT, Atlantic/Azores |
I | Whether it is daylight saving time | If it is daylight saving time, it is 1, otherwise it is 0 |
O | Hours from Greenwich Mean Time | For example: 0200 |
P | The difference from Greenwich Mean Time (GMT), there is a colon separating hours and minutes (newly added in PHP 5.1.3) | For example: 02:00 |
T | The time zone where this machine is located | For example: EST, MDT ([Translator's Note] In full text format under Windows, such as Eastern Standard Time, the Chinese version will display China Standard Time). |
Z | Time difference offset in seconds. Time zone offsets west of UTC are always negative, and time zone offsets east of UTC are always positive. | -43200 to 43200 |
Full date/time | --- | --- |
c | Date in ISO 8601 format (new in PHP 5) | 2004-02-12T15:19:21 00:00 |
r | Date in RFC 822 format | For example: Thu, 21 Dec 2000 16:01:07 0200 |
U | Number of seconds since the Unix epoch (January 1 1970 00:00:00 GMT) | See time() |
getdate() function
This function returns date and time information in the form of an array. If there is no timestamp, the current time shall prevail.
元 素 |
说 明 |
seconds |
秒,返回值0~59 |
minutes |
分钟,返回值为0~59 |
hours |
小时,返回值为0~23 |
mday |
月份中第几天,返回值为1~31 |
wday |
星期中第几天,返回值为0(星期天)~6(星期六) |
mon |
月份数字,返回值为1~12 |
year |
4位数字表示的完整年份,返回值加2000或2008 |
yday |
一年中第几天,返回值0~365 |
weekday |
星期几的完整文本表示,返回值为Sunday~Saturday |
month |
月份的完整文本表示,返回值为January~December |
0 |
返回从UNIX纪元开始的秒数 |
Element
Description
Compare the size of two times
In actual development, we often encounter the problem of judging the size of two times. Times in PHP cannot be directly compared. Therefore, the time must first be output in timestamp format, and then compared. This is a commonly used method.
There are two functions that can achieve this function. The strtotime() function is used here, which can parse the date and time description of any English text into a UNIX timestamp. The syntax of this function is:
int strtotime(string time, int now)
This function has two parameters. If the format of the parameter time is an absolute time, the now parameter has no effect; if the format of the parameter time is a relative time, then the corresponding time is provided by the parameter now. If the parameter now is not provided, the corresponding time is the current time. If parsing fails, -1 is returned.
Calculate the running time of the page script
Search engines are often used when browsing websites. When searching for information, careful users will find that at the bottom of the search results, there is usually the words "Search time is...seconds". The microtime() function is used here, which returns the current UNIX timestamp and microseconds. Returns a string in the format msec sec, where sec is the current UNIX timestamp and msec is the number of microseconds. The format of this function is:
string microtime(void)
; echo There are still $sub2 days before the opening of the Beijing Olympics!!!; $end_time = run_time(); echo 'Time consuming'.($end_time - $start_time); ?>