In PHP development, we often judge the size of two times, but in PHP, two times is not directly comparable, because the time is composed of years, months, days, hours, minutes, seconds, so if you need to two time into Line comparison, the first thing we have to do is to parse the time into a timestamp format, which will be used in front of us to learn the use ofThe strtotime () function resolves the date and time to a UNIX timestamp knowledge, only the time is converted to the time stamp of the lattice can be compared. This chapter will explain to you,
in PHP, how to compare the size of two times.
If there are now two time:
2017-4-15
2018-4-15
We'll first use the Strtotime () function to convert these two times into timestamps.
Strtotime ("2017-4-15")
Strtotime ("2018-4-15")
Then we compare the size of these two timestamps, so that we can compare the size of our two time, let us use examples to illustrate
Compare size instances of two times
The code is as follows
<?phpheader ("Content-type:text/html;charset=utf-8"); Set the encoding $time1=date ("y-m-d h:i:s"); Get current time $time2= "2017-5-1 12:30:00"; Set a time for the variable $time2 echo "variable \ $time 1 of the time:". $time 1. " <br/> "; The time of the output $time1 echo "variable \ $time 2 is:". $time 2. " <br/> "; Time of output $time2 if (Strtotime ($time 1)-strtotime ($time 2<0)) { //differential operation for two difference echo "\ $time 1 earlier than \ $time 2"; Time1-time2<0, stating time1 time in front}else{ echo "\ $time 2 earlier than \ $time 1"; Otherwise, the time2 time is in front}?>
Running results such as:
Detailed Examples:
In this example, we first declare two time variables, and then use the Strtotime () function to compare the two variables, then make the difference, and finally output the results based on the difference. This makes it possible to compare the size of two times.
implements two times the size comparison, the next section, we use the time-date function, Complete the function of a countdown.