In the process of doing the project, very frequently encountered the problem of time, like the comparison of time, a specific time to perform a certain action, but now only solve a part of the problem, first explain the question of time judgment.
Very simple, time, constantly make date (), now (), is a string type, cannot be directly compared, it needs to be converted to int, here is a function--strtotime (), used to convert date () to int.
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:
<?php $zero 1=date ("y-m-d h:i:s"); $zero 2 = "2010-11-29 21:07:00′; echo "Zero1 Time:" $zero 1. " <br> "; echo "Zero2 time:" $zero 2. " <br> "; if (Strtotime ($zero 1) <strtotime ($zero 2)) { 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
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:
<?php $zero 1=strtotime (Date ("y-m-d h:i:s")); Current Time $zero 2=strtotime ("2011-2-03 24:00:00′); Chinese New Year time $guonian =ceil (($zero 2-$zero 1)/86400); 60s*60min*24h echo "<strong> $guonian </strong> days from New Year's Day! ";?>
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, if this parameter is not provided, the current time of the system.
Time Judgment of thinkphp