Compare the difference between the two time values in PHP _ PHP Tutorial

Source: Internet
Author: User
In PHP, compare the difference between the two time values and the date values. Here, we use the timestamp mktime (hour, minute, second, month, day, year, [is_dst]). its parameters can be left or right, any omitted parameter will be set as the local date and time stamp.

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.


When mktime (hour, minute, second, month, day, year, [is_dst]), its parameters can be omitted from right to left, any omitted parameter will be set to the cost date and time...

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.