Php Date and Time Processing-zheng AQI. For more information, see.
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
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.
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"
";
?>