PHP provides a large number of built-in functions, allowing developers to easily process time, greatly improving work efficiency. Today, we will introduce some common PHP date and time functions as well as date and time processing. 9.1 Common date and time Processing Functions Table 9-1: Common date and... SyntaxHighlighter. all ();
PHP provides a large number of built-in functions, allowing developers to easily process time, greatly improving work efficiency. Today, we will introduce some common PHP date and time functions as well as date and time processing.
9.1 Common date and time processing functions
Table 9-1: Common date and time processing functions
Letter count
Description
Checkdate
Verify the time function to determine whether the time is valid. if the time is valid, true is returned; otherwise, false is returned.
Date_default_timezone_get
Obtains the default time zone used by the date and time functions of the script.
Date_default_timezone_set
Set the default time zone of the DATETIME function
Date
Format a local time/date
Getdate
Get date/time information
Gettimeofday
Get current time
Localtime
Get local time
Microtime
Returns the current timestamp and the number of microseconds.
Mktime
Obtain a UNIX timestamp
Strtotime
Parses the date and time description of any English text into a UNIX timestamp
Time
Returns the current UNIX timestamp.
9.2 Processing date and time
9.2.1 obtain the current date and time: date () function. usage:
Date (string format, int timestamp)
This function returns the string generated by the timestamp parameter in the specified format. The timestamp parameter is optional. if it is omitted, the current time is used. The format parameter allows developers to output time and date in the specified format.
Date_default_timezone_set (PRC); // Set Beijing time.
1. year-month-day
Echo date ('Y-m-J'); // example: 2007-02-6
Echo date ('Y-n-J'); // example: 07-2-6
Uppercase Y indicates four digits of the year, while lowercase y indicates two digits of the year;
Lowercase m indicates the number of the month (with the leading digit), while lowercase n indicates the number of the month without the leading digit.
Echo date ('Y-M-J'); // example: 2007-Feb-6
Echo date ('Y-m-D'); // example: 2007-02-06
Uppercase M indicates the three abbreviations of the month, while lowercase m indicates the number of the month (with leading 0 );
There is no upper-case J. only lower-case j indicates the date of the month, and there is no leading o. if the month needs to have the upper-case J, the lower-case d is used.
Echo date ('Y-M-J'); // example: 2007-Feb-6
Echo date ('Y-F-js'); // example: 2007-February-6
Uppercase M indicates the three abbreviated characters of the month, while uppercase F indicates the full English writing of the month. (No lower case f)
Uppercase S indicates the suffix of a date, such as "st", "nd", "rd", and "th". for details, see the date number.
Summary:
Y indicates that the year can be in uppercase or lowercase y;
Indicates that the month can be written in uppercase letters (F), uppercase letters (M), lowercase letters (m), and lowercase letters (n );
Indicates the date suffix.
2, hour: minute: Second
By default, PHP interprets the display time as "Greenwich Mean Time", which is 8 hours different from our local time.
Echo date ('G: I: s a'); // example: 5: 56: 57 am
Echo date ('H: I: s a'); // example: 05: 56: 57 AM
Lowercase g indicates the 12-hour system, with no leading 0, while lowercase h indicates the 12-hour system with leading 0.
When the 12-hour format is used, it indicates that upper afternoon, lower case a indicates lower case "am" and "pm", and upper case A indicates upper case "AM" and "PM ".
Echo date ('G: I: s'); 14:02:26
Uppercase G indicates the number of hours in the 24-hour format, but does not contain the leading value. uppercase H indicates the number of hours in the 24-hour format.
Summary:
The letter g indicates that the hour does not contain a leading character, and the letter h indicates that the hour contains a leading character;
Lowercase g and h are in 12-hour format, while uppercase G and H are in 24-hour format.
3, leap year, week, day
Capital L indicates whether to determine the leap year of the year. if it is true, 1 is returned; otherwise, 0 is returned;
Lowercase l indicates the day of the week in full (Tuesday );
However, uppercase D is used to represent the three abbreviation of the day of the week (Tue ).
Echo date ('w'); // example: Today's week: 2
Echo date ('w'); // for example, this week is the 06th week of the year.
Lowercase w indicates the day of the week, expressed in numbers
In upper case, W indicates the number of weeks in a year.
Echo date ('t'); // example: this month is 28 days
Echo date ('Z'); // example: Today is the 36th day of this year
Lowercase t indicates the number of days in the current month
Lowercase z indicates that today is the day of the year
4. others
Echo date ('t'); // example: UTC
Uppercase T indicates the time zone setting of the server
Echo date ('I'); // example: 0
If I is used to determine whether the current value is success, 1 is returned for true. otherwise, 0 is returned.
Echo date ('u'); // example: 1170769424
The upper-case u table shows the total number of seconds from January 1, January 1, 1970 to the present, which is the Unix timestamp of the UNIX time era.
Echo date ('C'); // example: 2007-02-06T14: 24: 43 + 00: 00
Lowercase c represents the ISO8601 date, the date format is YYYY-MM-DD, with the letter T to interval the date and time, the time format is HH: MM: SS, the time zone uses Greenwich Mean Time (GMT).
Echo date ('r'); // example: Tue, 06 Feb 2007 14:25:52 + 0000
Lowercase r indicates the RFC822 date.
9.2.2 obtain date information: getdate () function
Syntax:
Array getdate (int timestamp)
This function returns date and time information in array format. if there is no timestamp, the current time prevails. The description of the joined array elements returned by this function is shown in Table 9-2:
Table 9-2: Description of the joined array elements returned by the getdate () function
Yuansu
Description
Seconds
Seconds, return value 0 ~ 59
Minutes
Minutes. the returned value is 0 ~ 59
Hours
Hour, return value is 0 ~ 23
Mday
The day of the month. the returned value is 1 ~ 31
Wday
The day of the week. the returned value is 0 (Sunday )~ 6 (Saturday)
Mon
Number of months. the returned value is 1 ~ 12
Year
The full year represented by four digits. The return value is 2000 or 2008.
Yday
The day of the year, the return value is 0 ~ 365
Weekday
Indicates the complete text of the day of the week. the returned value is Sunday ~ Saturday
Month
The complete text of the month. the returned value is January ~ December
0
Returns the number of seconds from the UNIX epoch.
Example:
$ Arr = getdate ();
Echo $ arr [year]. "-". $ arr [mon]. "-". $ arr [mday]. "";
Echo $ arr [hours]. ":". $ arr [minutes]. ":". $ arr [seconds]. "". $ arr [weekday];
Echo"
";
Echo "Today is the $ arr [yday] th of year ";
?>
Effect:
9.3 UNIX timestamp
The timestamp is the time when file attributes are created, modified, and accessed. Digital time stamp service (DTS) is one of the security services for web websites. it can protect the date and time of electronic files.
9.3.1 What is a timestamp
The timestamp is an encrypted credential document. it consists of three parts:
2. the files whose timestamps need to be added are encrypted using Hash codes to form a summary.
2. date and time information of the file accepted by DTS.
2. encrypt accepted DTS files.
The digital time is added by the authentication unit DTS, which is based on the time when DTS receives the file.
The principle of timestamp is to convert the value of time to the encrypted value through other encryption methods. after the time changes, the encrypted value also changes.
The advantage of the timestamp is that the changed encryption value can prevent unauthorized reuse of the value after it is stolen, which plays a role in encryption. The timestamp mainly depends on time and generates a unique value within the specified period of time.
9.3.2 obtain the local timestamp: mktime () function
Syntax:
Int mktime (int hour, int minute, int month, int day, int year, int [is_dst])
Table 9-3: mktime () function parameter description
Parameter
Description
Hour
Hours
Minute
Minutes
Second
Seconds (within one minute)
Month
Number of months
Day
Days
Year
Number of copies per year
Is_dst
The is_dst parameter can be set to 1 during the period. if not, it is set to 0. if you are not sure whether it is a period, it is set to-1 (default)
Note: the valid timestamp typically ranges from 20:45:54 to GMT ~ January 19, 2038 03:13:07 (this range conforms to the minimum and maximum values of 32-bit signed integers ). In Windows, the range is from January 1, January 1, 1970 ~ May January 19, 2038.
Example:
Echo "timestamp returned by the mktime function:". mktime ()."
";
Echo "the current date is:". date ("Y-m-d", mktime ())."
";
Echo "the current time is:". date ("H: I: s", mktime ());
?>
Effect:
9.4 system time zone settings
During the learning process, many students found that the time obtained through the date () function is different from the local time. This is because PHP5 has rewritten the date () function, the current date and time functions are eight hours less than the system time. In PHP, the standard Greenwich Mean Time (zero time zone) is set by default ).
There are two ways to change the time zone settings in PHP:
1. modify the settings in the php. ini file, find the; date. timezone = option under [date], change this option to date. timezone = Asia/Hong_Kong, and restart the apache server.
2. in the application, add the following functions before using the time and date functions:
Date_default_timezone_set ("Asia/Hong_Kong ");
After the setting is complete, the date () function can be used normally without any time difference.
9.5 date and time problems encountered during time development
9.5.1 compare the two time ranges
In actual development, we often encounter two time sizes. the time in PHP cannot be compared directly. Therefore, the time must be output as the timestamp format before Comparison. this is a common method.
Two functions can implement this function. here we use the strtotime () function, which can resolve the datetime description of any English text to a UNIX timestamp. The syntax of this function is:
Int strtotime (string time, int now)
This function has two parameters. If the time format of the parameter is absolute time, the now parameter does not work. if the time format of the parameter is relative time, the corresponding time is provided by the parameter now, if the now parameter is not provided, the corresponding time is the current time. If the parsing fails,-1 is returned.
Example:
$ Time1 = date ("Y-m-d H: I: s"); // Obtain the current time
$ Time2 = "16:30:00"; // set a time for the variable $ time2
Echo "variable/$ time1 Time:". $ time1 ."
"; // Output two time variables
Echo "variable/$ time2 Time:". $ time2 ."
";
If (strtotime ($ time1)-strtotime ($ time2) <0) {// compare the two times
Echo "/$ time1 earlier than/$ time2"; // if the time1-time2 <0 indicates the time before time1
} Else {
Echo "/$ time2 earlier than/$ time1"; // otherwise, the time before time2
}
?>
Effect:
9.5.2 calculate the difference between two dates
In addition to comparing the sizes of two dates, the strtotime () function can also precisely know the difference between the two dates. The following uses a countdown applet to explain how to use the strtotime () function to calculate the difference between two dates.
$ Time1 = strtotime (date ("Y-m-d H: I: s "));
$ Time2 = strtotime ("17:10:00 ");
$ Time3 = strtotime ("2008-8-8 ");
$ Sub1 = ceil ($ time2-$ time1)/3600); // 60*60
$ Sub2 = ceil ($ time3-$ time1)/86400); // 60*60*24
Echo "$ sub1 hours out of the holiday !!! ";
Echo"
";
Echo "$ sub2 days away from Beijing Olympics opening !!! ";
?>
Effect:
9.5.3 calculate the running time of the page script
Search engines are often used when browsing websites. when searching for information, careful users will find that at the bottom of the search results, there are generally "the search time is ...... Second.
The microtime () function is used here, which returns the current UNIX timestamp and the number of microseconds. The string in the format of msec sec, where sec is the current UNIX timestamp and msec is the microseconds. The function format is:
String microtime (void)
The following code calculates the running time of the previous example:
Function run_time ()
{
List ($ msec, $ sec) = explode ("", microtime ());
Return (float) $ msec + (float) $ sec );
}
$ Start_time = run_time ();
$ Time1 = strtotime (date ("Y-m-d H: I: s "));
$ Time2 = strtotime ("17:10:00 ");
$ Time3 = strtotime ("2008-8-8 ");
$ Sub1 = ceil ($ time2-$ time1)/3600); // 60*60
$ Sub2 = ceil ($ time3-$ time1)/86400); // 60*60*24
Echo "$ sub1 hours out of the holiday !!! ";
Echo"
";
Echo "$ sub2 days away from Beijing Olympics opening !!! ";
$ End_time = run_time ();
?>
The running time of this example is Seconds