PHP: Calculate Real Differences Between Two Dates or Timestamps

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

dateDiff function example usage

strtotime examples:


UNIX timestamp and precision examples


Converting text format back to UNIX timestamp example


Share this post


  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:

      • Thank you very much!!!
        it worked perfectly!!!

  5. Hi,

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

    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.


      • Thank you again!!!

        This helped me a lot!!!

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


    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:



  1. php date difference accurate multiformat input and output ideas for design, code and seo - [...] recently came across a very simple solution detailed on the following site, PHP: Calculate Real Differences Between Two Dates …

Submit a Comment

Your email address will not be published. Required fields are marked *