Example of how PHP implements time comparison and time difference calculation.

Source: Internet
Author: User

Example of how PHP implements time comparison and time difference calculation.

This article describes how to implement time comparison and Time Difference Calculation in PHP. We will share this with you for your reference. The details are as follows:

Example 1:

<? Php // PHP time comparison and time difference calculation: // (1 ). compare the two absolute time headers ("Content-type: text/html; charset = UTF-8"); date_default_timezone_set ('prc '); $ zero1 = date ("Y-m-d h: I: s"); // $ zero1 = "21:07:00"; $ zero2 = "21:07:00 "; echo "zero1 time :". $ zero1. "<br>"; echo "zero2 time :". $ zero2. "<br>"; // strtotime-parses the datetime description of any English text into a Unix timestamp if (strtotime ($ zero1) <strtotime ($ zero2 )) {echo "zero1 earlier than zero2";} else if (strtotime ($ zero1)> str Totime ($ zero2) {echo "zero2 earlier than zero1";} else {echo "zero2 equals zero1";} echo "<br/>";?>

Running result:

Zero1 time: 12: 18: 39zero2 time: 21: 07: 00zero2 earlier than zero1

Example 2:

<? Php // (2 ). countdown applet $ zero1 = strtotime (date ("y-m-d h: I: s"); // current time, note that H is 24 hours h is 12 hours $ zero2 = strtotime ("00:00:00"); // Chinese new year's time // float ceil (float $ value) // return the next integer not less than value. If the value has a decimal part, it is entered. $ Guonian = ceil ($ zero2-$ zero1)/86400 ); // 60 s * 60 min * 24 hecho "<strong> $ guonian </strong> days before the Chinese New Year! "; Echo" <br/> ";?>

Running result:

There are 161 days before the Chinese New Year

Example 3:

<? Php // (3 ). PHP calculates two Time Difference Methods $ startdate = date ("y-m-d H: I: s"); $ enddate = "18:00:00 "; // floor-rounding method to get the integer // float floor (float $ value) // returns the nearest integer not greater than the value, and rounds the fractional part. $ Date = floor (strtotime ($ enddate)-strtotime ($ startdate)/86400); $ hour = floor (strtotime ($ enddate)-strtotime ($ startdate )) % 86400/3600); $ minute = floor (strtotime ($ enddate)-strtotime ($ startdate) % 86400/60); $ second = floor (strtotime ($ enddate) -strtotime ($ startdate) % 86400% 60); echo "there is still a period from the End Time ". $ date. "Day ". $ hour. "Hour ". $ minute. "Minute ". $ second. "seconds"; echo "<br/>";?>

Running result:

Now there are 6 days, 5 hours, 339 minutes, 56 seconds from the end time.

PS: here we recommend several time and date related tools for your reference:

Online date/day calculator:
Http://tools.jb51.net/jisuanqi/date_jisuanqi

Online date calculator/days calculator:
Http://tools.jb51.net/jisuanqi/datecalc

Online date-days difference calculator:
Http://tools.jb51.net/jisuanqi/onlinedatejsq

Unix timestamp Conversion Tool:
Http://tools.jb51.net/code/unixtime

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.