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

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: 10

1 8 9 10
    1. Hello sir.
      Please Guide Me How To Calculate Time Difference
      For Example.
      $t1 = strtotime(‘2011/09/19 10:00:22 AM’);
      $t2 = strtotime(‘2011/09/22 11:30:18 PM’);
      $delta_T = ($t2 – $t1);
      $day = round(($delta_T % 604800) / 86400);
      $hours = round((($delta_T % 604800) % 86400) / 3600);
      $minutes = round(((($delta_T % 604800) % 86400) % 3600) / 60);
      $sec = round((((($delta_T % 604800) % 86400) % 3600) % 60));
      echo $day.” days”.””;
      echo $hours.” hours”.””;
      echo $minutes.” minutes”.””;
      echo $sec.” sec”.””;

      This Is my php code
      output is:
      3 days
      3 hours
      60 minutes
      56 sec

      But my need This Output:

      • Hi Divyesh,

        I don’t absolute understand what try to get. How this could be converted to 2:30?

        Btw. your code is problematic, if this your real output. It should be:
        3 days
        4 hours
        56 sec

        And you might have problems with longer time spans too.

    2. Thanks! working great

    3. Thank you sharing this code. worked for me. thanks

1 8 9 10

Leave a Reply to Tim Cancel reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.