PHP two time difference between the number of days, hours, minutes, seconds and detailed examples of code _php example

Source: Internet
Author: User

PHP calculates the number of days, hours, minutes, seconds of two time difference

PHP calculates two time difference days, hours, minutes, seconds is not as convenient as other languages, but it is easy to find out the way of time in PHP. This article explains how PHP calculates the time difference. Need the Code farmers can refer to.

Let's look at the following example on the Internet:

$startdate is the start time, $enddate is the end time
<?php
$startdate = "2011-3-15 11:50:00";

$enddate = "2012-12-12 12:12:12";

$date =floor ((Strtotime ($enddate)-strtotime ($startdate))/86400);
echo "Days off:". $date. " Days <br/><br/> ";

$hour =floor ((Strtotime ($enddate)-strtotime ($startdate))%86400/3600);
echo "Hour difference:". $hour. " Hours <br/><br/> ";

$minute =floor ((Strtotime ($enddate)-strtotime ($startdate))%86400/60);
echo "Number of minutes:". $minute. " Minutes <br/><br/> ";

$second =floor ((Strtotime ($enddate)-strtotime ($startdate))%86400%60);
echo "Number of seconds:". $second. " Seconds ";
? >

Whether it's the type of time you're constructing with a string (using Strtotime conversion) or the type of time you get directly using the system's timing function, it's actually a variable of long shaping. Two such variables, it is obvious that you can do subtraction.

Do subtraction to get the value is the difference in the number of seconds, this number of seconds to 86400 (the number of seconds in a day), then get the difference. If modulo 86400 is taken, 3,600 seconds, 60 seconds, and then the number of hours and minutes are associated. If modulo 86400 is taken and 60 is modulo, the difference in seconds is obtained.

Thank you for reading, I hope to help you, thank you for your support for this site!

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.