Use the Strtotime () function to compare the size of a two-time instance

Source: Internet
Author: User
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.

Related Article

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.