I was using simple function to calculate difference between two dates and timestamps until I noticed, it’s not working correctly in long intervals. It’s very easy to calculate difference between two timestamps in seconds, but it’s much more complicated print difference in human readable format. The Internet can be found in a wide range of ways to do this thing, but as a rule they use a fixed amount of seconds for the year and the month. So if we calculate year with using 365 or 365.25 days and month using 30 or 31 then the difference is not accurate, because of leap years, DST (Daylight Saving Time) and so on.

Because of this problem, I decided to make a function (at least in the short testing) to return the right kind of differences between the UNIX timestamps and dates in human readable format. This function uses PHP strtotime function to calculate real differences and **can handle leap years and DST**. This function **can also return Twitter like about texts** with precision parameter.

## PHP dateDiff function for calculating real differences between dates and UNIX timestamps

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
<?php // Set timezone date_default_timezone_set("UTC"); // Time format is UNIX timestamp or // PHP strtotime compatible strings function dateDiff($time1, $time2, $precision = 6) { // If not numeric then convert texts to unix timestamps if (!is_int($time1)) { $time1 = strtotime($time1); } if (!is_int($time2)) { $time2 = strtotime($time2); } // If time1 is bigger than time2 // Then swap time1 and time2 if ($time1 > $time2) { $ttime = $time1; $time1 = $time2; $time2 = $ttime; } // Set up intervals and diffs arrays $intervals = array('year','month','day','hour','minute','second'); $diffs = array(); // Loop thru all intervals foreach ($intervals as $interval) { // Create temp time from time1 and interval $ttime = strtotime('+1 ' . $interval, $time1); // Set initial values $add = 1; $looped = 0; // Loop until temp time is smaller than time2 while ($time2 >= $ttime) { // Create new temp time from time1 and interval $add++; $ttime = strtotime("+" . $add . " " . $interval, $time1); $looped++; } $time1 = strtotime("+" . $looped . " " . $interval, $time1); $diffs[$interval] = $looped; } $count = 0; $times = array(); // Loop thru all diffs foreach ($diffs as $interval => $value) { // Break if we have needed precission if ($count >= $precision) { break; } // Add value and interval // if value is bigger than 0 if ($value > 0) { // Add s if value is not 1 if ($value != 1) { $interval .= "s"; } // Add value and interval to times array $times[] = $value . " " . $interval; $count++; } } // Return string with times return implode(", ", $times); } ?> |

## dateDiff function example usage

**strtotime examples:**

0 1 2 3 4 5 6 |
echo dateDiff("2010-01-26", "2004-01-26") . "\n"; echo dateDiff("2006-04-12 12:30:00", "1987-04-12 12:30:01") . "\n"; echo dateDiff("now", "now +2 months") . "\n"; echo dateDiff("now", "now -6 year -2 months -10 days") . "\n"; echo dateDiff("2009-01-26", "2004-01-26 15:38:11") . "\n"; |

**Output:**

0 1 2 3 4 5 6 |
6 years 18 years, 11 months, 30 days, 23 hours, 59 minutes, 59 seconds 2 months 6 years, 2 months, 10 days 4 years, 11 months, 30 days, 8 hours, 21 minutes, 49 seconds |

**UNIX timestamp and precision examples**

0 1 2 3 4 |
echo dateDiff(time(), time()-1000000, 1) . "\n"; echo dateDiff(time(), time()-1000000, 3) . "\n"; echo dateDiff(time(), time()-1000000, 6) . "\n"; |

**Output:**

0 1 2 3 4 |
11 days 11 days, 13 hours, 46 minutes 11 days, 13 hours, 46 minutes, 40 seconds |

**Converting text format back to UNIX timestamp example**

0 1 2 3 4 5 6 |
$time1 = time(); $time2 = $time1-10000000; echo $diff = dateDiff($time1, $time2) . "\n"; echo $time1 . "\n"; echo strtotime(" +".$diff, $time2) . "\n"; |

**Output:**

0 1 2 3 4 |
3 months, 23 days, 17 hours, 46 minutes, 40 seconds 1264514564 1264514564 |

Thanks (Y)

;)

hi..nice work and thanks for sharing…i would like to know how to translate the month days year into another language

hi how can i translate years months days etc.. in another language please ?

See my gist, it allows to add translation: https://gist.github.com/danielwolf1/cb0516fc6c180c4106c0

Can’t get it to work. Can you please explain how to use it?

Thanks for your perfect difference of dates.

And what is if one of the two dates have a null in the database?

Hi Marcel Epp,

If you have nulls in database, then you have to handle that case, like:

Thank you very much!!!

it worked perfectly!!!

Hi,

it’s me again. How can get an output like

00:12:39?

I have to put more of these outputs together to get the time from all.

Station1 = 00:01:20

Station2 = 00:02:15

Station3 = 01:34:10

all togehter = 01:37:45

Hi,

This function doesn’t do it, but you can try to convert all

hours, minutes and seconds to secondsand convert total sum of seconds back to hours, minutes and seconds.Example:

Thank you again!!!

This helped me a lot!!!

Hello! I’m using your function for a date difference and works very well, but I must add another variable is an extratime in minutes. Is there any way to use your function for doing this operation?

Example:

Start : 14/09/2015 10:03:05

Suspension for x minutes: from 14/09/2015 11:04:39 for 27 minutes

Stop : 14/09/2015 11:14:02

Result: 1 hour and 11 minutes

Instead I need 1 hour and 11 minutes – 27 minutes = The real time worked.

thank you in advantage

Hi Alessandro,

Here is one possible way, if you can convert that suspension time to seconds:

Updated the function a bit and added it as a Gist on Github:

https://gist.github.com/davidjeddy/362ff2565b1af1259e12

Good Script, Thanks Bro

Thanks for script, can i reblog this for indonesian programmers especially codeignter lovers ! my blog is here http://www.sidaurukfreddy.com

For example

6 years

19 years

2 months

6 years, 2 months

5 years

Total=?? how to add total year?

it is output in loop, mean one person have experience in different period then how to find total year experience?

Hello Muhaammad, each diff date you should put in avariable then count it,

Hey I really like this function but If I need to only display the different hours between working hours, how would I do that,

I’m think something like this, but The interval loops got me a little confused, I know this try to increment start from year but I always need to know if the time is in working hours.

$time = date(“Hi”, strtotime(‘+’.$num.’ minutes’));

$day = date(“D”, strtotime(‘+’ . $num . ‘ minutes’));

if($day != ‘Sat’ && $day != ‘Sun’ && $time >= 800 && $time <= 1800)

{

Could you help please?

Thanx Man! Love you…you save my day….you are best one