PHP time and date, PHP time and date

Source: Internet
Author: User
Tags iso 8601 iso 8601 format

PHP time and date, PHP time and date

PHP provides a large number of built-in functions, allowing developers to easily process time, greatly improving work efficiency. This section describes some common PHP date and time functions as well as date and time processing.


Common date and time processing functions

Letter count

Description

Checkdate

Verify the time function to determine whether the time is valid. If the time is valid, true is returned; otherwise, false is returned.

Date_default_timezone_get

Obtains the default time zone used by the date and time functions of the script.

Date_default_timezone_set

Set the default time zone of the datetime Function

Date

Format a local time/date

Getdate

Get date/time information

Gettimeofday

Get current time

Localtime

Get local time

Microtime

Returns the current Timestamp and the number of microseconds.

Mktime

Obtain a UNIX Timestamp

Strtotime

Parses the date and time description of any English text into a UNIX Timestamp

Time

Returns the current UNIX timestamp.


System Time Zone settings

During the learning process, we found that the time obtained through the date () function is different from the local time. This is because PHP5 has rewritten the date () function. Therefore, the current date and time functions are eight hours less than the system time. In PHP, the standard Greenwich Mean Time (Zero Time Zone) is set by default ). There are two ways to change the time zone settings in PHP:
1. Modify the settings in the php. ini file, find the; date. timezone = option under [date], change this option to date. timezone = Asia/Hong_Kong, and restart the apache server.
2. In the application, add the following functions before using the time and date functions:
Date_default_timezone_set("Asia/Hong_Kong"); after the setting is complete, the date () function can be used normally without any time difference.


UNIX Timestamp

The timestamp is the time when file attributes are created, modified, and accessed. Digital time stamp service (DTS) is one of the security services for web websites. It can protect the date and time of electronic files.

The timestamp is an encrypted credential document. It consists of three parts:
2. The files whose timestamps need to be added are encrypted using Hash codes to form a summary.
2. Date and time information of the file accepted by DTS.
2. Encrypt accepted DTS files.
The digital time is added by the authentication unit DTS, which is based on the time when DTS receives the file. The principle of timestamp is to convert the value of time to the encrypted value through other encryption methods. After the time changes, the encrypted value also changes. The advantage of the timestamp is that the changed encryption value can prevent unauthorized reuse of the value after it is stolen, which plays a role in encryption. The timestamp mainly depends on time and generates a unique value within the specified 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

Seconds (within one minute)

Month

Number of months

Day

Days

Year

Number of copies per year

Is_dst

The is_dst parameter can be set to 1 during the period. If not, it is set to 0. If you are not sure whether it is a period, it is set to-1 (default)

Note: The valid timestamp typically ranges from 20:45:54 to GMT ~ January 19, 2038 03:13:07 (this range conforms to the minimum and maximum values of 32-bit signed integers ). In Windows, the range is from January 1, January 1, 1970 ~ May January 19, 2038.


Date () function

Date (string format, int timestamp)
This function returns the string generated by the timestamp parameter in the specified format. The timestamp parameter is optional. If it is omitted, the current time is used. The format parameter allows developers to output time and date in the specified format.
Date_default_timezone_set (PRC); // Set Beijing time.

formatCharacter Description Return Value example
Day --- ---
D The day of the month, which has two digits leading to zero 01To31
D The day of the week. The text indicates three letters. MonToSun
J The day of the month, with no leading zero 1To31
L(Lowercase letters of "L) Day of week, complete text format SundayToSaturday
N The day of the week represented by a number in the ISO-8601 format (New in PHP 5.1.0) 1(Monday)7(Sunday)
S English suffix after the number of days per month, 2 Characters St,Nd,RdOrTh. AndJUse together
W The day of the week, represented by a number 0(Sunday)6(Saturday)
Z The day of the year. 0To365
Week --- ---
W The week of the year in ISO-8601 format, starting from Monday each week (New in PHP 4.1.0) For example:42(The first week of the year)
Month --- ---
F The month in the complete text format, such as January or March. JanuaryToDecember
M Number indicates the month, with a leading zero 01To12
M The month abbreviated to three characters. JanToDec
N Number indicates the month, with no leading zero 1To12
T Number of days in a given month 28To31
Year --- ---
L Is it a leap year? If the leap year is1Otherwise0
O The number of years in ISO-8601 format. This andYExcept for the week number of ISO (W) Belongs to the previous year or the next year, that year is used. (New PHP 5.1.0) Examples:1999Or2003
Y The year in which the four digits represent the complete number. For example:1999Or2003
Y A two-digit year For example:99Or03
Time --- ---
A Morning and afternoon values in lower case AmOrPm
A Upper-case morning and afternoon values AMOrPM
B Swatch Internet standard 000To999
G Hour, 12-hour format, no leading zero 1To12
G Hour, in 24-hour format, no leading zero 0To23
H Hour, 12-hour format, with leading zero 01To12
H Hour, in 24-hour format, with a leading zero 00To23
I Minutes with a leading zero 00To59>
S Number of seconds, with a leading zero 00To59>
U Millisecond (New in PHP 5.2.2 ). Note thatDate ()Function always returns000000It only accepts integer Parameters, while DateTime: format () supports milliseconds. Example:654321
Time Zone --- ---
E Time zone ID (New in PHP 5.1.0) For example:UTC,GMT,Atlanta/Azores
I When it is enabled or not If it is enabled1Otherwise0
O Hours different from Greenwich Mean Time For example:+ 0200
P Difference from Greenwich Mean Time (GMT), separated by colons between hours and minutes (New in PHP 5.1.3) For example:+ 0: 00
T Time zone of the Local Machine For example:EST,MDT([Note] the full text format is in Windows, for example, "Eastern Standard Time". The Chinese version displays "Chinese Standard Time ").
Z The number of seconds of the time difference offset. The Time Zone offset to the west of UTC is always negative, and the time zone offset to the east of UTC is always positive. -43200To43200
Complete 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 Unix epoch (January 1 1970 00:00:00 GMT) See time ()

Getdate () function

This function returns date and time information in array format. If there is no timestamp, the current time prevails.

Yuansu

Description

Seconds

Seconds, return value 0 ~ 59

Minutes

Minutes. The returned value is 0 ~ 59

Hours

Hour, return value is 0 ~ 23

Mday

The day of the month. The returned value is 1 ~ 31

Wday

The day of the week. The returned value is 0 (Sunday )~ 6 (Saturday)

Mon

Number of months. The returned value is 1 ~ 12

Year

The full year represented by four digits. the return value is 2000 or 2008.

Yday

The day of the year, the return value is 0 ~ 365

Weekday

Indicates the complete text of the day of the week. The returned value is Sunday ~ Saturday

Month

The complete text of the month. The returned value is January ~ December

0

Returns the number of seconds from the UNIX epoch.


Compare the two time ranges

In actual development, we often encounter two time sizes. The time in PHP cannot be compared directly. Therefore, the time must be output as the timestamp format before comparison. This is a common method.
Two functions can implement this function. Here we use the strtotime () function, which can resolve the datetime description of any English text to a UNIX timestamp. The syntax of this function is:
Int strtotime (string time, int now)
This function has two parameters. If the time format of the parameter is absolute time, the now parameter does not work. If the time format of the parameter is relative time, the corresponding time is provided by the parameter now, if the now parameter is not provided, the corresponding time is the current time. If the 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 are generally "the search time is ...... Second. The microtime () function is used here, which returns the current UNIX Timestamp and the number of microseconds. The string in the format of msec sec, where sec is the current UNIX Timestamp and msec is the microseconds. The function format is:
String microtime (void)

<! Doctype html> 


Copyright Disclaimer: This article is an original article by the blogger Lang Yun studio and cannot be reproduced without the permission of the blogger.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.