PHP Time and date

Source: Internet
Author: User
Tags echo date php language iso 8601 iso 8601 format local time time and date

PHP provides a large number of built-in functions that enable developers to work on time, greatly improving productivity.

Describes some common PHP date and time functions and the processing of dates and times.


Frequently used date and time processing functions

function

Description

Checkdate

Validates the time function. Infers whether the time is valid, returns true correctly, otherwise returns false

Date_default_timezone_get

Gets the default time zone used by the script datetime function

Date_default_timezone_set

Set the default time zone for datetime functions

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

Get a Unix timestamp

Strtotime

Parse the date-time descriptive narrative of any English text into a UNIX timestamp

Time

Returns the current UNIX timestamp


System Time zone settings

During the learning process, it was found that the time obtained through the date () function is different from the local time, because PHP5 has rewritten the date () function, so that the current datetime function is 8 hours less than the system time.

The default setting in the PHP language is the standard GMT (that is, the zero zone). There are two ways to change the time zone settings in the PHP language:
1. Change the settings in the php.ini file to find [date], date.timezone = option, change the item to Date.timezone=asia/hong_kong, and then start Apacheserver again.
2. In the application, use the time-date function before adding for example the following function:
Date_default_timezone_set ("Asia/hong_kong"); once set, the date () function is ready to use and no more time-lag problems occur.



Unix time stamp

Timestamps are the creation, alteration, and access times in file properties. Digital time stamp service, DTS, is one of the Web site security services projects that provide security for the date and time information of electronic files.

A timestamp is a document that has been encrypted and formed after it has been created. It consists of 3 parts:
2 files that need to be timestamp are encrypted with a hash code to form a digest.
2 DTS accepts the date and time information for the file.
2 Encryption of the accepted DTS files.
The digital time is added by the Authentication unit DTS. The time that DTS received the file is based on. The function of time stamp is to convert the value of time into an encrypted value by other encryption method, and the value of encryption will change after time change.

The advantage of time stamp is: The change of encryption value to prevent the value is stolen after illegal reuse, also played the role of encryption. Timestamps are mainly dependent on time and produce a unique value for a specified period of time.



Mktime () function

Grammar:
int mktime (int hour, int minute, int month, int day, int year, int [IS_DST])

   number

say   

Hour

hours

minute

minutes

second

seconds (within one minute)

month

Number of months

Day

days

Year

Number of years

Is_dst

Parameters is_dst in daylight saving time can be set to 1. Set to 0 if not determined if daylight saving time is set to 1 (default)

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


Date () function

Date (string format,int timestamp)
The function returns the number of timestamp strings that are generated in accordance with the specified format. The parameter timestamp is optional, assuming omitted, the current time is used.

The format parameter allows the developer to output the time and date in the form it specifies.
Date_default_timezone_set (PRC); Set the GMT.

Example of a
format character description return value
Day --- ---
D The day ordinal of a month with a leading zero 2-digit number to
D Day of the week, text representation, 3 letters Mon to Sun
J The day ordinal of the month. No leading zeros 1 to
l(lowercase letter of "L") Day of the week, full text format Sunday to Saturday
N ISO-8601 format number represents the day of the week (PHP 5.1.0 new Plus) 1(for Monday) to 7(= Sunday)
S English suffix, 2 characters, after the number of days per month St,nd. Rd or th. Can be used with J .
W Day of the week, numbers indicate 0(for Sunday) to 6(= Saturday)
Z The day ordinal of the year 0 to 365
Week --- ---
W Week of the ISO-8601 format year, starting from Monday (PHP 4.1.0 New) For example: (42nd week of the year)
Month --- ---
F Month. Complete text format, such as January or March January to December
M A number represents the month, with a leading zero to
M Three letter abbreviation for month Jan to Dec
N The month represented by the number. No leading zeros 1 to
T The number of days that a given month should be from
Years --- ---
L Whether it is a leap year A leap year is assumed to be 1, otherwise 0
O ISO-8601 format year number. This is the same value as Y . Only if the number of weeks (W) of the ISO is assumed to belong to the previous or next year, then that year.

(PHP 5.1.0 new addition)

Examples: 1999 or 2003
Y 4-digit year in full representation For example:1999 or 2003
Y Year 2-digit representation For example :
Time --- ---
A Lowercase morning and afternoon values am or pm
A Uppercase morning and afternoon values AM or PM
B Swatch Internet Standard Time to 999
G Hours, 12-hour format, no leading zeros 1 to
G Hours. 24-hour format. No leading zeros 0 to
H Hours, 12-hour format, with leading zeros to
H Hours, 24-hour format, with leading zeros From xx to
I Number of minutes with leading zeros From xx to >
S Number of seconds. With leading zeros From xx to >
U Milliseconds (PHP 5.2.2 new Plus). It is important to note that the date () function always returns 000000 because it accepts only integer parameters. and Datetime::format () only supports milliseconds.

Demo Sample: 654321
Time --- ---
E Time zone ID (PHP 5.1.0 new addition) For example:UTC,GMT,atlantic/azores
I Whether it is daylight saving time Let's say daylight saving time is 1, otherwise 0
O Hours of difference from GMT For example:+0200
P The difference from Greenwich mean GMT (GMT). Colon separated between hours and minutes (PHP 5.1.3 New addition) For example:+02:00
T The time zone in which this machine resides For example:EST,MDT("translator note" in Windows for the full text format, such as "Eastern Standard Time", the Chinese version will show "Chinese standards").
Z The number of seconds of the slack offset. The time zone offset in the west of UTC is always negative, and the time zone offset to the east of UTC is always positive. -43200 to 43200
Full Date/Time --- ---
C Date in ISO 8601 format (PHP 5 new) 2004-02-12t15:19:21+00:00
R Date in RFC 822 format For example:Thu, Dec 16:01:07 +0200
U The number of seconds since the Unix era (January 1 1970 00:00:00 GMT) started See Time ()

GETDATE () function

The function returns the DateTime information in the form of an array, assuming that there is no timestamp, whichever is the current time.


Element

Description

Seconds

Seconds. return value 0~59

Minutes

Minutes with a return value of 0~59

Hours

Hours. The return value is 0~23

Mday

The day ordinal of the month. The return value is 1~31

Wday

The first day of the week, the return value is 0 (Sunday) to (Saturday)

Mon

Month number, return value is 1~12

Year

4-digit full year, return value plus 2000 or 2008

Yday

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

Weekday

The full text representation of the day of the week, with a return value of Sunday~saturday

Month

The full text representation of the month. The return value is January~december

0

Returns the number of seconds since the Unix era


Two times the size of a

In the actual development often encountered inferred two time size, PHP time is not able to directly to do the comparison. Therefore, the time is first output to the timestamp format. And then the comparison, which is often used to the method.


There are two functions that can do this, using the strtotime() function, which resolves a datetime descriptive narrative of whatever English text to a Unix timestamp. The syntax for this function is:
int Strtotime (string time, int now)
The function has two parameters. Assume that the format of the parameter time is absolute. The now parameter does not work, assuming that the format of the parameter time is relative. Then the corresponding time is provided by the reference now, assuming that no parameters are provided, the corresponding time is the current time.

Assume that parsing failed. then return-1.


Calculate the execution time of a page script

When browsing the site, search engines are often used. When searching for information. The attentive user will find out. At the bottom of the search results, there is usually a "search time for ...". Seconds "of the word. The microtime() function is used here to return the current UNIX timestamp and the number of microseconds. Returns a string formatted as msec sec, in which the SEC is the current Unix timestamp and msec is the number of microseconds.

The format of the function is:
String Microtime (void)

<!doctype html>

PHP Time and date

Related Article

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.