Detailed explanation of PHP time function

  • 2021-12-04 09:34:33
  • OfStack

In the development of PHP, the use of time functions is basically ubiquitous, and there are many ways to operate time in PHP, such as PHP timestamp, conversion between date and timestamp, obtaining the current date, the time before or after the current time, etc. Let's talk about the use of various time functions in PHP in detail.

First of all, we need to know that the time acquisition method in php is date (), and the time stamp acquisition methods in php are time () and strtotime (). The following are described separately.

date () is formatted as date ($format, $timestamp), format is formatted, and timestamp is a timestamp (optional).

time () returns the Unix timestamp of the current time, with no arguments.

strtotime($time, $now) Resolves the formatted date-time or any English text date-time description to an Unix timestamp. $time is required, specifying the time string to be parsed; $now is used to calculate the timestamp of the return value, and if this parameter is omitted, the current time is used.

date($format) Examples of usage:

echo date('Y-m-d'); Output result: December 16, 2014 echo date('Y-m-d H:i:s'); Output result: 23:00:00 on December 16, 2014 echo date('Y年m月d日'); Output: December 16, 2014

In fact, you only need to modify the date format according to the rules. The following is the meaning of each letter in the string format:

a-"am" or "pm"

A-"AM" or "PM"

d-a few days, 2 digits, if less than 2 digits, the front is filled with zero; For example: "01" to "31"

D-Day of the week, 3 letters; For example: "Fri"

F-month, full English name; For example: "January"

h-12-hour system hours; For example: "01" to "12"

H-24-hour hours; For example: "00" to "23"

g-12-hour system hours, less than 2 bits do not fill zero; For example: "1" to 12 "

G-24-hour system hours, less than 2 bits do not fill zero; For example: "0" to "23"

i-min; For example: "00" to "59"

j-several days, 2 digits, if less than 2 digits do not fill zero; For example: "1" to "31"

l-Day of the week, full English name; For example: "Friday"

m-month, 2 digits, if it is less than 2 digits, it will be filled with zero in front; For example: "01" to "12"

n-month, 2 digits, if less than 2 digits, no zero; For example: "1" to "12"

M-month, 3 letters; For example: "Jan"

s-sec; For example: "00" to "59"

S-suffix with English ordinal number, 2 English letters; Such as "th" and "nd"

t-Specifies the number of days in the month; For example: "28" to "31"

U-Total seconds

w-Digital Day of the Week, E. G. "0" (Sunday) to "6" (Saturday)

Y-year, 4 digits; For example: "1999"

ES 113EN-year, 2 digits; For example: "99"

ES 116EN-Day of the year; For example: "0" to "365"

Examples of usage of time ():

time(); Output: 1418664250 (the returned result is the current timestamp)

Examples of usage of strtotime ($time):

echo strtotime ('2014-12-16'); Output result: 1418688000, which actually converts 2014-12-1600: 00:00 into UNIX timestamp

strtotime() There is also a very powerful usage, parameters can be added to the operation of numbers, year, month, day and week English characters, examples are as follows:

echo date ('Y-m-d H: i: s', strtotime ('+1 day')); Output: 2014-12-17 23:30:33 (output at this time tomorrow) echo date ('Y-m-d H: i: s', strtotime ('-day')); Output: 2014-12-15 23:30:33 (at this time yesterday) echo date ('Y-m-d H: i: s', strtotime ('+1 week')); Output: 2014-12-22 23:30:33 (get the time at this time next week) echo date ('Y-m-d H: i: s', strtotime ('next Thursday')); Output: 2014-12-1800: 00:00 (get the time of next Thursday morning) echo date ('Y-m-d H: i: s', strtotime ('last Thursday')); Output: 2014-12-11 00:00:00 (get the time of last Thursday morning)

So many examples above, more of their own flexible research, strtotime () method can control the display of Unix timestamp through English text, and get the required time and date format.

php Gets the number of milliseconds of the current time

php itself does not provide a function to return milliseconds, but provides the microtime () method, which returns an array of two elements: 1 is the number of seconds and 1 is the number of decimal milliseconds. We can get the number of milliseconds returned by this method as follows:


function getMillisecond(){
 list($s1,$s2)=explode(' ',microtime());
 return (float)sprintf('%.0f',(floatval($s1)+floatval($s2))*1000);
}

In PHP, the time obtained usually differs from the current time by 8 hours. This is because in the configuration of PHP, the time in time zone 0 is taken as the benchmark by default, while we are located in East Zone 8, which differs from time zone 0 by 8 hours. Therefore, when time is used in actual development, special attention should be paid to setting the time zone. The setting methods mainly include the following:

1. Find date. timezone in php. ini and change its value to Asia/Shanghai, that is date.timezone = Asia/Shanghai (Set the current time zone to Shanghai Asia Time Zone)

2. Add at the beginning of the program date_default_timezone_set('Asia/Shanghai'); Yes, of course, for Chinese time zones, we can also use date_default_timezone_set('PRC'); To set

Summarize


Related articles: