How can i find the difference between two time in php?

Possible Duplicate:
How to get time difference in minutes in PHP

I am working on an attendance table to calculate the late and very late employees. I am storing the login time in the table [Type: time]. I am able to get the time from the database and i would like to show the time difference in the separate column.

i.e., if employee logged on or before 09:00:59, then its right time and the time diff should be shown as null. If he logs in after the time i.e, 09:01:00 or later the time difference should be 00:00:01. Like wise i need to calculate the differences in time.

One time is constant i.e., 09:00:59 and another one i am getting from database table. Need to get diff between both. I am working in PHP. Hope my question is clear.

Thank you in Advance.

asked Dec 18, 2012 at 7:09

4

You can use strtotime[] for time calculation. Here is an example:

$checkTime = strtotime['09:00:59'];
echo 'Check Time : '.date['H:i:s', $checkTime];
echo '';

$loginTime = strtotime['09:01:00'];
$diff = $checkTime - $loginTime;
echo 'Login Time : '.date['H:i:s', $loginTime].'
'; echo [$diff < 0]? 'Late!' : 'Right time!'; echo '
'; echo 'Time diff in sec: '.abs[$diff]; echo ''; $loginTime = strtotime['09:00:59']; $diff = $checkTime - $loginTime; echo 'Login Time : '.date['H:i:s', $loginTime].'
'; echo [$diff < 0]? 'Late!' : 'Right time!'; echo ''; $loginTime = strtotime['09:00:00']; $diff = $checkTime - $loginTime; echo 'Login Time : '.date['H:i:s', $loginTime].'
'; echo [$diff < 0]? 'Late!' : 'Right time!';

Demo

Check the already-asked question - how to get time difference in minutes:

Subtract the past-most one from the future-most one and divide by 60.

Times are done in unix format so they're just a big number showing the number of seconds from January 1 1970 00:00:00 GMT

answered Dec 18, 2012 at 7:17

6

You can also use DateTime class:

$time1 = new DateTime['09:00:59'];
$time2 = new DateTime['09:01:00'];
$interval = $time1->diff[$time2];
echo $interval->format['%s second[s]'];

Result:

1 second[s]

answered Dec 18, 2012 at 7:49

DroneZzZkoDroneZzZko

4563 silver badges10 bronze badges

1


answered Dec 18, 2012 at 7:16

zafus_coderzafus_coder

4,1312 gold badges11 silver badges13 bronze badges

0

In this article, we will see how to calculate the difference between 2 dates in PHP, along with understanding its implementation through the examples. Given two dates ie., start_date and end_date & we need to find the difference between the two dates.

Consider the below example:

Input: start_date: 2016-06-01 22:45:00 
       end_date: 2018-09-21 10:44:01
Output: 2 years, 3 months, 21 days, 11 hours, 59 minutes, 1 seconds
Explanation: The difference of 2 dates will give the date in complete format.

Method 1: Using date_diff[] Function

This function is used to find the difference between two dates. This function will return a DateInterval object on the success and returns FALSE on failure.

Example: This example illustrates the use of the date_diff[] function to calculate the difference between the 2 dates.

PHP

Output:

+2 years 3 months

Method 2: To use the date-time mathematical formula to find the difference between two dates. It returns the years, months, days, hours, minutes, seconds between two specified dates.

Example: In this example, we will be using the date-time mathematical formula to calculate the difference between the dates that will be returned in years, months, days, hours, minutes, seconds.

PHP

Output:

2 years, 3 months, 21 days, 11 hours, 59 minutes, 1 seconds

Method 3: This method is used to get the total number of days between two specified dates.

PHP

Output:

Difference between two dates: 103

PHP is a server-side scripting language designed specifically for web development. You can learn PHP from the ground up by following this PHP Tutorial and PHP Examples.


How can I get the difference between two time in php?

php $start = strtotime["12:00"]; $end = // Run query to get datetime value from db $elapsed = $end - $start; echo date["H:i", $elapsed]; ?>

How do you find the difference in time between two times?

To find the difference between 2 times in a certain time unit, ignoring the others, use one of the following functions..
Difference in hours, ignoring minutes and seconds: =HOUR[B2-A2].
Difference in minutes, ignoring hours and seconds: =MINUTE[B2-A2].
Difference in seconds, ignoring hours and minutes: =SECOND[B2-A2].

Chủ Đề