PHP Time and date _php tutorial

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

PHP 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.

Common date and time processing functions

function

Description

Checkdate

Validates the time function, determines whether the time is valid, returns true correctly, or 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

Resolves a datetime description of any English text to 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 the current datetime function is 8 hours less than the system time. The standard GMT is set by default in the PHP language (that is, the zero zone is used). There are two main ways to change the time zone settings in the PHP language:
1. Modify the settings in the php.ini file, locate the [date], Date.timezone = option, modify the entry to Date.timezone=asia/hong_kong, and then restart the Apache server.
2. In the application, add the following function before using the time-date function:
Date_default_timezone_set ("Asia/hong_kong"); After Setup is complete, the date () function can be used normally, and there will be no more jet lag issues.

Unix time stamp

The timestamp is the creation, modification, and access time in the file properties. Digital time stamp service, DTS, is one of the Web site security services items that provide the security of date and time information for electronic files.

The timestamp is a document that has been encrypted and formed, and consists of 3 parts:
² files that need to add timestamps are encrypted with hash code to form a digest.
²dts Accept the date and time information for the file.
² Encrypt the accepted DTS files.
The digital time is added by the Authentication unit DTS, which is based on the time that DTS received the file. 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 the timestamp is that the variable encryption value prevents the value from being stolen illegally, and it plays 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])

Parameters

Description

Hour

Number of hours

Minute

Number of minutes

Second

Number of seconds (within one minute)

Month

Number of months

Day

Days

Year

Number of years

Is_dst

The parameter is_dst can be set to 1 for daylight saving time, if not set to 0, or 1 (default) if it is not determined if daylight saving time is set.

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 a string that is generated by the parameter timestamp in the specified format. Where the parameter timestamp is optional and if omitted, the current time is used. The format parameter allows the developer to output a time date in the form that it specifies.
Date_default_timezone_set (PRC); Set the GMT.

format character description return value Example
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 without leading zeros 1 to
l(Small 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, full 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 number represents the month, without leading zeros 1 to
T The number of days that a given month should be from
Years --- ---
L Whether it is a leap year If the leap year is 1, otherwise 0
O ISO-8601 format year number. This is the same value as Y , except if the day of the week (W) of the ISO belongs to the previous year or the next year. (PHP 5.1.0 new addition) Examples: 1999 or 2003
Y 4-digit year in full representation 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 the integer parameter, and Datetime::format () supports milliseconds. Example: 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 If daylight saving time is 1, otherwise 0
O Hours of difference from GMT Example:+0200
P Difference from GMT (GMT), between hours and minutes colon separated (PHP 5.1.3 New) Example:+02:00
T The time zone in which this machine resides For example:EST,MDT(the "translator note" is in full text format under Windows, for example Eastern Standard Time, Chinese version will show Chinese standards times).
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 datetime information in the form of an array, whichever is the current time if there is no timestamp.

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

Full text representation of the month with a return value of January~december

0

Returns the number of seconds since the Unix era


Compare the size of two times

In the actual development often encountered to determine the size of two times, PHP time is not directly to the comparison. Therefore, the first time to output a timestamp format, and then compare, this is a common method.
There are two functions that can be implemented, using the Strtotime () function, which resolves the datetime description of any English text to a Unix timestamp. The syntax for this function is:
int Strtotime (string time, int now)
The function has two parameters. If the format of the parameter time is absolute, then the now parameter does not work, and if the parameter time is formatted as a relative time, then its corresponding timing is provided by the parameter, and if no parameter is present, the corresponding time is the current time. If the parsing fails, 1 is returned.

Calculate the run time of a page script

When browsing the site, often use the search engine, in the search for information, careful users will find that at the bottom of the search results, generally have "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, where the SEC is the current Unix timestamp and msec is the number of microseconds. The format of the function is:
String Microtime (void)

<title></title> ; The difference between the current time and the Unix era, Echo times ().
; 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));
; Localization timestamp, strtotime can also be used to compare the size of time 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 = ru N_time (); $time 1 = strtotime (date (y-m-d h:i:s)); $time 2 = strtotime (2008-2-3-17:10:00); $time 3 = strtotime (2008-8-8); $sub 1 = ceil ($time 2-$time 1)/3600); $sub 2 = Ceil (($time 3-$time 1)/86400); 1 hours from holiday and $sub!!! ; Echo

; Echo from the opening of the Beijing Olympic Games and $SUB2 days!!!; $end _time = Run_time (); Echo ' time-consuming '. ($end _time-$start _time);?>



http://www.bkjia.com/PHPjc/1030446.html www.bkjia.com true http://www.bkjia.com/PHPjc/1030446.html techarticle php time and date PHP provides a large number of built-in functions that enable developers to work on time, greatly improving productivity. Introduce some common PHP date and time functions ...

  • 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.