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

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

1 2 3 4 5 10
    1. Works like a charm, I now have this the way I want it ;-)

      Thx JR !

      Reply
    2. You’re welcome Andy

      Nice to hear that you got it working as you wanted… :D

      Reply
    3. I found another used for your below code that I wanted to use it for, however, I found something quirky with it. Unless an else statment is used with it it only appears to work one day a year. By that I mean I had it set for 1999-08-20 and 11 showed as years passed but soon as it rolled midnight 8/20 the year print dissapeard. Using the below code which is the same as the original provided minus the default TZ since I have that set in my ini file what needs to be done to make the year always show if that makes sense. Like right now assuming 8-20 stays then for the next 360 some odd days it should show the printed 11 and change to a 12 come 8-20 of next year.

      Reply
    4. I guess I could else the if statement but there’s got to be an easier way.

      Reply
    5. It seems I solved the problem by removin the if statement all together, let me know if that was the wrong decision due to an easier solution… either way if I’m right the printed years past should now always show regardless of the day and still +1 a year when the $birthday_date rolls aorund. So like right now with it being on 1999-08-20 it shows 11 and should continue to show it for the next 360 some odd days and change to a 12 come 8-20 of next year and continue to roller over a new year for each passing year.

      Reply
    6. When I said I removed the if statement I meant just the if ($birthday_month == $current_month && $birthday_day == $current_day) { }, I left the echo in. But like I said, everything appears to be working ok now (the years passed always show and not just on the bday like when the if statement was included). Oh, sorry for all the posts to but there’s no edit function here ;-/

      Either way I really appreciate your code and the help given ;-)

      Oh, dumb question but was what I did by removing the if statement and leaving the echos what you would have suggested ?

      Reply
    7. Hi Andy,

      I’m not quite sure anymore what you want to do?

      If you remove that if-statement, then you show that birthday message every day. On the other hand, if you want to show the elapsed time all the time, then remove just that else-statement.

      Reply
    8. I got what I wanted I believe, like you said if I remove the if statement the birthday message or in this specific case the years passed will always show. Thats what I wanted to do with the demod linked page. This way the persons age will always show and auto increment with each passing birthday.

      http://www.myu2sig.com/interviews/arne_meyer_interview_0-0-0000.php

      What I originally asked about with the birthday message is used on my main site myu2sig.com where Happy x B-Day myu2sig.com ! shows up once a year on the sites birthday. Like I said in one of my messages, this new idea which you linked on the demo page is something I came up with the other night.

      Reply
    9. HELLO!, This datediff() function is awesome! Great Job =D

      I have a question, this is the code that i have from here but i modified just instead of giving years, motnhs, seconds.. it displays just the hours and minutes. My question is: How to output 355:14 instead of 355 hours, 14 minutes ?????

      This is my code (yours but modified):

      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(‘hour’,’minute’);
      $diffs = array();

      // Loop thru all intervals
      foreach ($intervals as $interval) {
      // Set default diff to 0
      $diffs[$interval] = 0;
      // Create temp time from time1 and interval
      $ttime = strtotime(“+1 ” . $interval, $time1);
      // Loop until temp time is smaller than time2
      while ($time2 >= $ttime) {
      $time1 = $ttime;
      $diffs[$interval]++;
      // Create new temp time from time1 and interval
      $ttime = strtotime(“+1 ” . $interval, $time1);
      }
      }

      $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);
      }

      THANKS IN ADVANCE =D

      Reply
      • Hi Daniel,

        Nice to hear that you like this function. :)

        Replace the code at the end with this:

        
        $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
          // if value is bigger than 0
          if ($value > 0) {
            // Add value to times array
            $times[] = $value;
            $count++;
          }
        }
        
        // Return string with times
        return implode(":", $times);
        

        Let me know if you do not get this to work?

        Reply
    10. It works THANKS, just 1 thing, when i get just minutes is it possible to get
      Example:
      00:02 = 2 minutes

      or if its hours to get:

      02:05

      Thanks in advance.. =D

      Reply
    11. i didn’t explained very well in my last question..
      i don’t want to get: “00:02 = 2 minutes”
      i want to get: “00:02”
      or if its hours to get: “02:05”
      Thanks in advance, you are awesome, u solved my first question very fast.

      Reply
    12. @Daniel

      Following should work:

      
      // Loop thru all diffs
      foreach ($diffs as $interval => $value) {
        // Break if we have needed precission
        if ($count >= $precision) {
          break;
        }
        // Fill with zeroes
        $times[] = str_pad($value, 2, "0", STR_PAD_LEFT);
        $count++;
      }
      
      Reply
    13. @JR

      i have 3 words for you:

      YOU ARE AWESOME!
      i wish i could be as good as you, i hope some day =D
      Many thanks to you.

      Reply
      • Thanks for the compliments! :D

        Reply
    14. Hey Jr, your script is great and your the best :D

      Reply
1 2 3 4 5 10

Leave a Reply to JR Cancel reply

Your email address will not be published.

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

Close