Compare the difference between the size of two time and date in PHP _php tutorial

Source: Internet
Author: User
We're all using timestamps here.

Mktime (HOUR,MINUTE,SECOND,MONTH,DAY,YEAR,[IS_DST])
Its parameters can be omitted from right to left, and any omitted parameters are set to the current value of the cost date and time.

Parameter description
Hour is optional. Specified hours.
Minute is optional. Specify minutes.
Second is optional. Specify seconds.
Month is optional. Specifies the number of months to be represented.
Day is optional. Prescribed days.
Year is optional. Prescribed year. On some systems, the legal value is between 1901–2038. However, there is no such limit in PHP Tutorial 5.
IS_DST is optional. If the time is in daylight saving time (DST), set to 1, otherwise set to 0 and if unknown, set to-1. Since 5.1.0, the IS_DST parameter has been discarded. Therefore, you should use the new Time zone processing feature


In our daily life, we often compare time with each other, and it is very simple for us to judge the size of time. But the comparison of time is not just a comparison of the number size, so it is relatively complex. So how do you compare the size of two times in PHP?

To compare the size of two times, we need to convert the time into a timestamp format and then compare it to the most common method. The functions commonly used are: Strtotime ()
Syntax format: strtotime (Time,now)
If time is absolute, then the now parameter does not work
If time is relative, then the corresponding function is now provided, and if it is not provided, then the corresponding time is the current local time.

Example: Compare the size of two absolute times
Code:

$zero 1=date ("y-m-d h:i:s");
$zero 2 = "2010-11-29 21:07:00′;
echo "Zero1 Time:" $zero 1. "
”;
echo "Zero2 time:" $zero 2. "
”;
if (Strtotime ($zero 1)<>
echo "Zero1 earlier than zero2′;
}else{
echo "Zero2 earlier than zero1′;
}
?>


Output Result:
Zero1 Time: 2010-11-30 21:12:55
Zero2 Time: 2010-11-29 21:07:00
Zero2 earlier than Zero1

Note: The thought can be divergent according to the instance

Calculates the difference between two dates
Countdown to the Olympic Games, the countdown to the Asian Games, and the countdown for birthdays can be achieved by calculating the difference of two dates, as well as using the Strottime () function.
The implementation of the countdown needs to be two time difference integer, need to use the function ceil ()
The function of the ceil () function is to find the smallest integer not less than the given real number

Example: Countdown applet
Instance code:

$zero 1=strtotime (Date ("y-m-d h:i:s"));//Current time
$zero 2=strtotime ("2011-2-03 24:00:00′); New Year time
$guonian =ceil (($zero 2-$zero 1)/86400); 60s*60min*24h
echo " $guonian days from the New Year!" ”;
?>


Output Result:
There are 66 days left for the New Year!

Strtotime () function parsing

Definition and usage
The strtotime () function resolves the datetime description of any English text to a Unix timestamp.

Grammar
Strtotime (time,now) parameter description
TIME specifies a string of times to parse.
Now is used to calculate the timestamp of the return value. If this argument is omitted, the current time is used.

Description
The function expects to accept a string containing the U.S. English date format and attempt to resolve it to a Unix timestamp (the number of seconds from January 1 1970 00:00:00 GMT) whose value is relative to the time given by the now parameter, and if this parameter is not provided, the current time of the system


http://www.bkjia.com/PHPjc/445352.html www.bkjia.com true http://www.bkjia.com/PHPjc/445352.html techarticle Here we all use timestamp mktime (HOUR,MINUTE,SECOND,MONTH,DAY,YEAR,[IS_DST]) whose parameters can be omitted from right to left, any omitted parameters will be set cost to 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.