# PHP: Calculate Real Differences Between Two Dates or Timestamps - Comment Page: 9

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...

## 226 comments on “PHP: Calculate Real Differences Between Two Dates or Timestamps - Comment Page: 9”

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

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

3. Thanks for your perfect difference of dates.

4. 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:

``````
function dateDiff(\$time1, \$time2, \$precision = 6) {

if (is_null(\$time1) || is_null(\$time2)) {
return false;
}

// If not numeric then convert texts to unix timestamps
...
}
``````
• Thank you very much!!!
it worked perfectly!!!

5. 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 seconds and convert total sum of seconds back to hours, minutes and seconds.

Example:

``````
``````
``` Reply ```
``` ```
``` Marcel Epp 14th October 2015, 1:07 pm Thank you again!!! This helped me a lot!!! Reply ```
• ``` ```
``` ```
6. ``` Alessandro 14th October 2015, 6:40 pm 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 Reply JR 22nd October 2015, 10:55 am Hi Alessandro, Here is one possible way, if you can convert that suspension time to seconds: --- orig.php 2015-10-22 09:10:33.936394437 +++ test.php 2015-10-22 09:08:35.088546417 @@ -5,7 +5,7 @@ // Time format is UNIX timestamp or // PHP strtotime compatible strings - function dateDiff(\$time1, \$time2, \$precision = 6) { + function dateDiff(\$time1, \$time2, \$suspension, \$precision = 6) { // If not numeric then convert texts to unix timestamps if (!is_int(\$time1)) { \$time1 = strtotime(\$time1); @@ -22,6 +22,10 @@ \$time2 = \$ttime; } + \$time1 = \$time1 + \$suspension; + // OR + //\$time2 = \$time2 - \$suspension; + // Set up intervals and diffs arrays \$intervals = array('year','month','day','hour','minute','second'); \$diffs = array(); @@ -69,3 +73,5 @@ // Return string with times return implode(", ", \$times); } + + echo dateDiff('2015-09-14 10:03:05', '2015-09-14 11:14:02', (27*60)) . "\n"; Reply David J Eddy 16th October 2015, 9:25 pm Updated the function a bit and added it as a Gist on Github: https://gist.github.com/davidjeddy/362ff2565b1af1259e12 Reply Muhammad Shahzad 6th January 2016, 7:57 am Good Script, Thanks Bro Reply Freddy 19th January 2016, 2:57 am Thanks for script, can i reblog this for indonesian programmers especially codeignter lovers ! my blog is here http://www.sidaurukfreddy.com Reply Muhammad Shahzad 19th January 2016, 9:35 am 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? Reply freddy 19th January 2016, 10:55 am Hello Muhaammad, each diff date you should put in avariable then count it, Reply Kristian 5th October 2016, 6:05 pm 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? Reply parm 6th October 2016, 4:16 pm Thanx Man! Love you…you save my day….you are best one Reply abofhdx5 25th December 2016, 10:22 am great work, i want ask about if i need output in Second , or minute ? i dont need hours , for example : In : 12:00 out : 1:05 dateDiff = 65 Minutes Reply rodney 24th February 2018, 3:12 am Thank you for this post it helps a lot, Reply ```
``` ```
``` « Previous 1 … 7 8 9 10 Next » Leave a Reply to boosoo Cancel replyYour email address will not be published. Required fields are marked *Comment * Name * Email * Website Δdocument.getElementById( "ak_js_1" ).setAttribute( "value", ( new Date() ).getTime() ); This site uses Akismet to reduce spam. Learn how your comment data is processed. ```
``` ```
``` ```
``` (adsbygoogle = window.adsbygoogle || []).push({}); Please support us and subscribe to inttf Youtube channel! ```
``` Last updated posts inttf NVIDIA 340.xx, 390.xx, 418.xx, 435.xx Patcher for Kernel 5.18/5.17/5.16/5.15/5.14/5.13 [BASH Script] inttf-kernel - Clear Linux kernel for Fedora 36 users Install NVIDIA Drivers [515.43.04 / 510.73.05 / 470.129.06 / 390.151 / 340.108] on CentOS Stream 9/8, RHEL 9/8, Rocky Linux 8.5 Fedora 36/35/34 NVIDIA [515.43.04 / 510.73.05 / 470.129.06 / 390.151 / 340.108] Drivers Install Guide Install NVIDIA [515.43.04 / 510.73.05 / 470.129.06 / 390.151 / 340.108] Drivers on Debian / Ubuntu / Linux Mint / LMDE Latest posts inttf-kernel - Clear Linux kernel for Fedora 36 users Upgrade to Fedora 36 from Fedora 35 using DNF Fedora 36 Workstation Installation Guide / Gnome 42 Quick Tour and New Features openSUSE Leap 15.4 Installation Guide [USB Media Creation, Multiboot, Gnome 41 Quick Tour] openSUSE 15.4 / 15.3 / Tumbleweed NVIDIA [515.43.04 / 510.73.05 / 470.129.06 / 390.151 / 340.108] Drivers Install Guide Most popular posts Fedora 36/35/34 NVIDIA [515.43.04 / 510.73.05 / 470.129.06 / 390.151 / 340.108] Drivers Install Guide VirtualBox 6.1.28 / 6.1.29 on Fedora 35/34/33, CentOS/RHEL 8/7 Install Skype 8.69 on Fedora 34/33, CentOS/RHEL/SL 8.3/7.8 Fedora 16 nVidia Drivers Install Guide (disable nouveau driver) Install Google Chrome on Fedora 36/35, CentOS Stream 9/8, Red Hat (RHEL) 9/8, Rocky Linux 8.5 © If Not True Then False | Terms of Service / Disclaimer | Privacy Policy ```
``` Go to mobile version Close function lCSS(src, onload = null){ var s = document.createElement('link'); s.href = src; s.rel = 'stylesheet'; s.type = 'text/css'; if (onload != null) { var a = document.createAttribute('onload'); a.value = onload; s.setAttributeNode(a); } document.getElementsByTagName('head')[0].appendChild(s); } //lCSS('https://fonts.googleapis.com/css?family=Lato:400,700', "document.documentElement.className+=' fontsloaded';"); lCSS('/wp-content/themes/inttf/css/prism.css'); lCSS('/wp-content/themes/inttf/css/baguetteBox.min.css'); document.querySelector('li.search a').addEventListener('click', function(){ var o = document.getElementById('sov'); var st = o.currentStyle ? o.currentStyle.display : getComputedStyle(o, null).display; if (st === "none") { o.style.display = "block"; } else { o.style.display = "none"; } }); document.querySelector('#sov .close').addEventListener('click', function(){ document.getElementById('sov').style.display = "none"; }); window.addEventListener('load', function() { if (typeof baguetteBox !== 'undefined') { baguetteBox.run('.entry-content'); } }); ```