Php Date and Time Processing-zheng AQI (continued)

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

1. UNIX Timestamp
Phpd processes data, especially when formatting data of the time type in the database, it is necessary to convert the data of the time type to the UNIX timestamp for processing. Time data of different Database Systems
Conversion is not compatible. In this case, the time must be converted to a UNIX timestamp. In this way, Beijing has achieved cross-platform performance for different database systems.
2. Convert time to Timestamp
If you want to convert the date and time expressed in a string into a timestamp, you can use the strtotime () function.
The syntax format is as follows:
Int strtotime (string $ time [, int $ now])
For example: Copy codeThe Code is as follows: <? Php
Echo strtotime ('2017-03-05 '); // output 2009
Echo strtotime ('2017-03-05 10:24:30 '); // output 2009
Echo strtotime ("10 September 2000"); // output 968544000
?>

Another UNIX timestamp function that gets the date is the mktime () function,
The syntax format is as follows:
Int mktime ([int $ hour [, int $ minute [, int $ second [, int $ month [, int $ day [, int $ year])
3. Get the date and time
1. data () function
Converts a timestamp to a specific date and time String Based on the given format.
The syntax format is as follows:
String date (string $ format [, int $ timestamp])
Note:
$ Format specifies the format of the converted date and time,
$ Timestamp is the timestamp to be converted. If it is omitted, the current local time is used, that is, the default value is the value of the time () function.
The time () function returns the timestamp of the current time.
The following table lists the values of the $ format parameter of the date function.
Table 4.6 date () Functions Support the format code

Character

Description

Return Value example

D

The day of the month, which has two digits leading to zero

01 ~ 31

D

The day of the week, represented by three letters

Mon to Sun

J

The day of the month, with no leading zero

1 ~ 31

L

Day of week, complete text format

Sunday ~ Saturday

N

The day of the week represented by a number in ISO-8601 format

1 (Monday )~ 7 (Sunday)

S

The English suffix after the number of days of each month, expressed in 2 Characters

St, nd, rd, or th, which can be used with j

W

The day of the week, represented by a number

0 (Sunday )~ 6 (Saturday)

Z

The day of the year.

0 ~ 366

W

Week of the Year in ISO-8601 format, starting from Monday

Example: 42 (42nd weeks of the current year)

F

The month in the complete text format, such as January or March.

January ~ December

M

Number indicates the month, with a leading zero

01 ~ 12

M

The month abbreviated to three characters.

Jan ~ Dec

N

Number indicates the month, with no leading zero

1 ~ 12

T

Number of days in a given month

28 ~ 31

L

Is it a leap year?

If the leap year is 1, otherwise it is 0

O

The number of years in ISO-8601 format. This is the same as Y, but if the week number (W) of ISO belongs to the previous year or the next year, the year is used.

Example: 1999 or 2003

Y

The year in which the four digits represent the complete number.

Example: 1999 or 2003

Y

A two-digit year

Example: 99 or 03

A

Morning and afternoon values in lower case

Am or pm

A

Upper-case morning and afternoon values

AM or PM

B

Swatch Internet standard

000 ~ 999

G

Hour, 12-hour format, no leading zero

1 ~ 12

G

Hour, in 24-hour format, no leading zero

0 ~ 23

H

Hour, 12-hour format, with leading zero

01 ~ 12

H

Hour, in 24-hour format, with a leading zero

00 ~ 23

I

Minutes with a leading zero

00 ~ 59

S

Number of seconds, with a leading zero

00 ~ 59

E

Time zone sign

Example: UTC, GMT, Atlanta/Azores

I

When it is enabled or not

If this parameter is set to 1, otherwise it is set to 0.

O

Hours different from Greenwich Mean Time

For example: + 0200

P

Difference from Greenwich Mean Time (GMT), separated by colons between hours and minutes

Example: +

T

Time zone of the Local Machine

Example: EST, MDT

Z

The number of seconds at the time zone 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.

-43200 ~ 43200

C

Date in ISO 8601 format

2004-02-12T15: 19: 21 + 00: 00

R

Date in RFC 822 format

Thu, 21 Dec 2000 16:01:07 + 0200

U

The number of seconds since the UNIX epoch.

Time () function

2. getdate () function
You can obtain an array of date and time information,
The syntax format is as follows:
Array getdate ([int $ timestamp])
Note: $ timestamp is the timestamp to be converted. If not given, the current time is used.
The function returns an array containing date and time information based on $ timestamp. The key names and values of the array are shown in Table 4.7.

Key name

Description

Value examples

Seconds

Number of seconds

0 ~ 59

Minutes

Minute numeric representation

0 ~ 59

Hours

Number of hours

0 ~ 23

Mday

Number of the day of the month

1 ~ 31

Wday

Number of the day of the week

0 (representing Sunday )~ 6 (Saturday)

Mon

Number of months

1 ~ 12

Year

The full year represented by four digits

Example: 1999 or 2003

Yday

Number of the day of the year

0 ~ 365

Weekday

Complete Text representation of the day of the week

Sunday ~ Saturday

Month

Complete Text representation of the month

January ~ December

0

The number of seconds since the UNIX epoch.

System related. Typical values range from-2147483648 ~ 2147483647

4.6.4 other Date and Time Functions
1. Calculation of date and timeCopy codeThe Code is as follows: <? Php
$ Oldtime = mktime );
$ Newtime = mktime );
$ Days = ($ newtime-$ oldtime)/(24*3600); // calculates the number of days for the two time ranges
Echo $ days; // output 18
?>

2. Check date
The checkdate () function can be used to check whether a date data is valid. The syntax format is as follows:
Bool checkdate (int $ month, int $ day, int $ year)Copy codeThe Code is as follows: <? Php
Var_dump (checkdate (12, 31, 2000); // output bool (TRUE)
Var_dump (checkdate (2001,); // output bool (FALSE)
?>

3. Set the time zone
The system defaults to the GMT standard time, so the current time may be different from the local time. PHP provides the date_default_timezone_set () function to modify the time zone (),
The syntax format is as follows:
Bool date_default_timezone_set (string $ timezone_identifier)
The $ timezone_identifier parameter is the time zone to be specified,
Available values in mainland China are Asia/Chongqing, Asia/Shanghai, Asia/Urumqi (in the order of Chongqing, Shanghai, and Urumqi ). PRC can be used in Beijing time.
4.5 instance-generate calendarCopy codeThe Code is as follows: <? Php
$ Year = @ $ _ GET ['Year']; // obtain the year of the address bar
$ Month = @ $ _ GET ['month']; // obtain the month in the address bar
If (empty ($ year ))
$ Year = date ("Y"); // initialize the year of this year
If (empty ($ month ))
$ Month = date ("n"); // initialize to the month of the current month
$ Day = date ("j"); // obtain the number of days of the current day
$ Wd_ar = array ("day", "one", "two", "three", "four", "five", "Six"); // array of weeks
$ Wd = date ("w", mktime (0, 0, 0, $ month, 1, $ year); // calculates the day of the week on the first day of the month.
// Year Link
$ Y_lnk1 = $ year <= 1970? $ Year = 1970: $ year-1; // previous year
$ Y_lnk2 = $ year> = 2037? $ Year = 2037: $ year + 1; // next year
// Monthly Link
$ M_lnk1 = $ month <= 1? $ Month = 1: $ month-1; // last month
$ M_lnk2 = $ month> = 12? $ Month = 12: $ month + 1; // next month
Echo "<table cellpadding = 6 cellspacing = 0 width = 200 bgcolor = # eeeeee> <tr align = center bgcolor = # cccccccc> ";
// Output year, click the "<" link to jump to the previous year, click the ">" link to jump to the next year
Echo "<td colspan = 4> <a href = 'ex4 _ 15.php? Year = $ y_lnk1 & month = $ month '>
</A> ". $ year." <a href = 'ex4 _ 15.php? Year = $ y_lnk2 & month = $ month '>></a> </td> ";
// Output the month, click the "<" link to jump to the previous month, click the ">" link to jump to the next month
Echo "<td colspan = 3> <a href = 'ex4 _ 15.php? Year = $ year & month = $ m_lnk1 '>
</A> ". $ month." month <a href = 'ex4 _ 15.php? Year = $ year & month = $ m_lnk2 '>></a> </td> </tr> ";
Echo "<tr align = center> ";
For ($ I = 0; $ I <7; $ I ++)
{
Echo "<td> $ wd_ar [$ I] </td>"; // outputs the array of weeks.
}
Echo "</tr> ";
$ Tnum = $ wd + date ("t", mktime (0, 0, 0, $ month, 1, $ year); // calculates the day of the week plus the day of the month
For ($ I = 0; $ I <$ tnum; $ I ++)
{
$ Date = $ I + 1-$ wd; // The Position of the calculated number of days in the table
If ($ I % 7 = 0) echo "<tr align = center>"; // start of a row
Echo "<td> ";
If ($ I >=$ wd)
{
If ($ date = $ day & $ month = date ("n") // if it is the current day of the month, the number of days is blacklisted.
Echo "<B>". $ day. "</B> ";
Else
Echo $ date; // number of output days
}
Echo "</td> ";
If ($ I % 7 = 6) echo "</tr>"; // end a row
}
Echo "</table> ";
?>

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.