The timestamp is used here.
Mktime (hour, minute, second, month, day, year, [is_dst])
The parameter can be omitted from the right to the left, and any omitted parameter will be set to the current value of the local date and time.
Parameter description
Hour is optional. The specified hour.
Minute is optional. Minutes.
Second is optional. Specified seconds.
Month is optional. Indicates the month in number.
Day is optional. Specified day.
Year is optional. Specified year. In some systems, the valid value ranges from 1901 to 2038. However, this restriction does not exist in php tutorial 5.
Is_dst is optional. If the time is in the daylight saving time (dst) period, it is set to 1; otherwise, it is set to 0. If it is unknown, it is set to-1. The is_dst parameter has been deprecated since 5.1.0. Therefore, the new time zone processing feature should be used.
In daily life, we often compare the time sooner or later. It is very easy for us to determine the size of time. However, the comparison of time is not just a comparison of numbers, so it is relatively complicated. In php, how does one compare the two time ranges?
To compare the two time values, we need to convert the time to the timestamp format and then compare them. This is the most common method. Commonly used functions: strtotime ()
Syntax format: strtotime (time, now)
If time is absolute time, the now parameter does not work.
If time is relative time, the corresponding parameter is provided by the corresponding function now. If the now parameter is not provided, the corresponding time is the current local time.
Instance: compare the two absolute time values
Code:
$ Zero1 = date ("y-m-d h: I: s ");
$ Zero2 = "21:07:00 ′;
Echo "zero1 time:". $ zero1 ."
";
Echo "zero2 time:". $ zero2 ."
";
If (strtotime ($ zero1) Echo "zero1 earlier than zero2 ′;
} Else {
Echo "zero2 earlier than zero1 ′;
}
?>
Output result:
Zero1 time: 21:12:55
Zero2 time: 21:07:00
Zero2 is earlier than zero1
Note: divergent thinking can be performed based on instances.
Calculate the difference between two dates
Olympics countdown, Asian Games Countdown, and birthday countdown can all be achieved by calculating the difference between the two dates. The strottime () function is also required.
To implement the countdown, the difference between the two time values must be integer, And the ceil () function must be used ()
The ceil () function is used to obtain the minimum integer not less than the given real number.
Example: Countdown Applet
Instance code:
$ Zero1 = strtotime (date ("y-m-d h: I: s"); // The current time
$ Zero2 = strtotime ("24:00:00 '); // Chinese new year's time
$ Guonian = ceil ($ zero2-$ zero1)/86400); // 60 s * 60 min * 24 h
Echo
$ GuonianDay !";
?>
Output result:
There are still 66 days before the Chinese New Year!
Strtotime () function Parsing
Definition and usage
The strtotime () function parses the date and time descriptions of any English text into unix timestamps.
Syntax
Strtotime (time, now) parameter description
Time specifies the time string to be parsed.
Now is used to calculate the timestamp of the returned value. If this parameter is omitted, the current time is used.
Description
This function is expected to accept a string that contains the American English Date Format and try to parse it into a unix timestamp (seconds since January 1 1970 00:00:00 gmt ), the value is relative to the time given by the now parameter. If this parameter is not provided, the current time of the system is used.