Date and time functions of php learning Notes

Source: Internet
Author: User
Tags echo date getdate local time

Obtaining the date and time is also frequently used in website design. Php provides the date () time function to implement this function. The structure is as follows:

Instance:

The code is as follows: Copy code

<? Php
Echo date ("F j, Y, g: I a"). "& nbsp ";
Echo date ("m. d. y"). "& nbsp ";
Echo date ("j, n, Y"). "& nbsp ";
Echo date ("Ymd"). "& nbsp ";
Echo date ('H-I-s, j-m-y, it is w Day Z'). "& nbsp ";
 
Echo date ('It is the jS day. '). "& nbsp ";
 
Echo date ("d m j G: I: s T Y"). "& nbsp ";
Echo date ('H: m: s m is month'). "& nbsp ";
 
Echo date ("H: I: s"). "& nbsp ";
Echo date ('l dS of f y h: I: s ');
Echo date (DATE_ATOM, mktime (0, 0, 0, 7, 1, 2000 ));
?>

Format parameter and its meaning
Parameters Description Remarks
A Morning or afternoon Am or pm
A Morning or afternoon AM or PM
B Internet time 000 ~ 999
D The day of the month. 01 ~ 31
D Replace with the abbreviation of the day of the week Mon ~ Sun
F Represents the month in English. January ~ December
G Hours 1 ~ 12
G Hours 0 ~ 23
H Hours 01 ~ 12
H Hours 00 ~ 23
I Minutes 00 ~ 59
I When it is enabled or not Yes is 1, no is 0
J The day of the month. 1 ~ 31
L Day of the week Sunday ~ Saturday
L Is it a leap year? Leap year is 1, not leap year is 0
M Use a number to represent the month. 01 ~ 12
M The abbreviation of a month. Jan ~ Dec
N Months are output in Arabic numerals 1 ~ 12
S Seconds 00 ~ 59
S English suffix of days per month 1st ~ 2nd
T Number of days in a specified month 28 ~ 31
T Time zone set on the local machine Such as EST
W The day of the week (from 0 ~ 6) 0 ~ 6
Y Replace with the 4-digit year number For example, 2011
Y Replace it with the 2-digit year number For example, 11
Z Day of the year 0 ~ 366
Z Time zone deviation in seconds -43200 ~ 43200

Getdate () definition and usage

The getdate () function obtains the date/time information.

Syntax
Getdate (timestamp) parameter description
Optional. Specifies the time in Unix time format.

Description
Returns a combined array containing date information based on timestamp. If no timestamp is provided, it is considered as the current local time.

The elements in the array are as follows:

The code is as follows: Copy code

<? Php
Print_r (getdate ());
?> Output:

Array
(
[Seconds] => 45
[Minutes] => 52
[Hours] => 14
[Mday] => 24
[Wday] => 2
[Mon] => 1
[Year] = & gt; 2006
[Yday] => 23
[Weekday] => Tuesday
[Month] => January
[0] = & gt; 1138110765
)

Key name Description Return value example
"Seconds" Number of seconds 0 to 59
"Minutes" Minute numeric representation 0 to 59
"Hours" Number of hours 0 to 23
"Mday" Number of the day of the month 1 to 31
"Wday" Number of the day of the week 0 (Sunday) to 6 (Saturday)
"Mon" Number of months 1 to 12
"Year" The full year represented by four digits Example: 1999 or 2003
"Yday" Number of the day of the year 0 to 365
"Weekday" Complete text representation of the day of the week Sunday to Saturday
"Month" Complete text representation of the month January to December
0 The number of seconds since the beginning of the Unix era, similar to the return value of time () and the value used for date. System related. The typical value is from-2147483648 to 2147483647.

Mktime () definition and usage

The mktime () function returns the Unix timestamp of a date.

The parameter always represents the GMT date, so is_dst has no effect on the result.

The parameters can be left empty from right to left. The blank parameters are set to the corresponding current GMT value.

Syntax
Mktime (hour, minute, second, month, day, year, is_dst)

Example
The mktime () function is very useful for date calculation and verification. It can automatically correct out-of-range input:

The code is as follows: Copy code
<? Php
Echo (date ("M-d-Y", mktime )));
Echo (date ("M-d-Y", mktime )));
Echo (date ("M-d-Y", mktime )));
Echo (date ("M-d-Y", mktime )));
?>

Output:

Jan-05-2002
Feb-01-2002
Jan-01-2001
Jan-01-1999

Parameters Description
Hour Optional. The specified hour.
Minute Optional. Minutes.
Second Optional. Specified seconds.
Month Optional. Indicates the month in number.
Day Optional. Specified day.
Year Optional. Specified year. In some systems, the valid value ranges from 1901 to 2038. However, this restriction does not exist in PHP 5.
Is_dst

Optional. If the time is in the daylight saving time (DST) period, it is set to 1; otherwise, it is set to 0. If it is unknown, it is set to-1.

The is_dst parameter has been deprecated since 5.1.0. Therefore, the new time zone processing feature should be used.

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.