Php date and time processing-Zheng AQI. For more information, see.
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:
The code is as follows:
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 time
The code is as follows:
$ 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)
The code is as follows:
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 calendar
The code is as follows:
$ 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"
";// Output year, click the "<" link to jump to the previous year, click the ">" link to jump to the next yearEcho"
<". $ Year." year> | ";// Output the month, click the "<" link to jump to the previous month, click the ">" link to jump to the next monthEcho"
<". $ Month." month> |
";Echo"
";For ($ I = 0; $ I <7; $ I ++){Echo"
$ Wd_ar [$ I] | "; // Outputs the array of weeks.}Echo"
";$ Tnum = $ wd + date ("t", mktime (0, 0, 0, $ month, 1, $ year); // calculates the day of the week plus the day of the monthFor ($ I = 0; $ I <$ tnum; $ I ++){$ Date = $ I + 1-$ wd; // The position of the calculated number of days in the tableIf ($ I % 7 = 0) echo"
"; // Start of a rowEcho"
"; 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"". $ Day .""; Else Echo $ date; // Number of output days } Echo" | ";If ($ I % 7 = 6) echo"
"; // End a row}Echo"
";
?>