Php time and date processing functions

Source: Internet
Author: User
Tags current time getdate iso 8601 iso 8601 format local time php tutorial time and date

1. UNIX timestamp
Php Tutorial d to process data, especially when formatting the time data in the database tutorial, you must first convert the time data 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 the code 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 time
Copy the code 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 the code 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 calendar
Copy the code 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> ";
?>

 

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.