Home > Backend Development > PHP Tutorial > PHP Time and Date_PHP Tutorial

PHP Time and Date_PHP Tutorial

WBOY
Release: 2016-07-13 09:46:48
Original
925 people have browsed it

PHP Time and Date

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

函 数

说 明

checkdate

验证时间函数,判断时间是否有效,有效返回true,否则返回false

date_default_timezone_get

取得脚本日期时间函数所使用的默认时区

date_default_timezone_set

设定日期时间函数的默认时区

date

格式化一个本地时间/日期

getdate

获取日期/时间信息

gettimeofday

获取当前时间

localtime

获取本地时间

microtime

返回当前时间戳和微秒数

mktime

取得一个UNIX时间戳

strtotime

将任何英文文本的日期时间描述解析为UNIX时间戳

time

返回当前的UNIX时间戳

Function

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

参 数

说 明

hour

小时数

minute

分钟数

second

秒数(一分钟之内)

month

月份数

day

天数

year

年份数

is_dst

参数is_dst在夏令时可以被设置为1,如果不是则设置为0;如果不确定是否为夏令时则设置为-1(默认值)

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)
Note: The typical range of valid timestamps is December 13, 1901 20:45:54 GMT to January 19, 2038 03:13:07 (this range conforms to the minimum and maximum values ​​​​of 32-bit signed integers) . In Windows systems, this range is limited to January 1, 1970 to January 19, 2038.

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

seconds seconds, return value 0~59 minutes minutes, the return value is 0~59 hours Hour, the return value is 0~23 mday The day of the month, the return value is 1~31 wday The day of the week, the return value is 0 (Sunday) ~ 6 (Saturday) mon Month number, the return value is 1~12 year Complete year represented by 4 digits, return value plus 2000 or 2008 yday The day of the year, return value 0~365 weekday The complete text representation of the day of the week, the return value is Sunday~Saturday month The complete text representation of the month, the return value is January~December 0 Returns the number of seconds since the UNIX epoch

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)

Copy after login
; // The difference between the current time and the UNIX epoch echo time().
; // Format time echo date(Y-m-d H-i-s).
; // Time array $arrays = getdate(); print_r($arrays).
; // Detection time var_dump( checkdate(7, 9, 2015) );
; // Localized timestamp, strtotime can also be used for time size comparison setlocale(LC_TIME, 0); echo strftime('%Y,%m,%d', strtotime('2000-04-00')).
; // Subtle echo microtime(true); function run_time() { list($msec, $sec) = explode( , microtime()); return ((float)$msec (float)$sec); } $start_time = run_time(); $time1 = strtotime(date( Y-m-d H:i:s)); $time2 = strtotime(2008-2-3 17:10:00); $time3 = strtotime(2008-8-8); $ sub1 = ceil(($time2 - $time1) / 3600); //60 * 60 $sub2 = ceil(($time3 - $time1) / 86400); //60 * 60 * 24 echo There is still time before the holiday $sub1 hour!!! ; echo

; echo There are still $sub2 days before the opening of the Beijing Olympics!!!; $end_time = run_time(); echo 'Time consuming'.($end_time - $start_time); ?>



www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1030446.htmlTechArticlePHP Time and Date PHP provides a large number of built-in functions, allowing developers to handle time with ease, greatly improving Improve work efficiency. Introducing some common PHP date and time functions...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template