Page 1 of 1

Time difference

Posted: Mon Dec 27, 2004 2:01 pm
by josh
I'm lookin for a function to format time difference.
Like format(timestamp) would return how long timestamp is, like format(10) would return 10 seconds... format(120) returns 2 minutes.
I would code it myself if it weren't for the complications of different days in months, leap years, etc.. because if the time i give it is 2 years and a day i want it to return 2 Years, 1 Day

I would be using it on a forum system to show how old each post is
(posted 5 minutes ago/posted 3 months, 2 weeks, 4 days ago)

Something like that
is there any classes or functions out there to do this?
I would also want it to round off, like if the time is over a week dont bother displaying the minutes and seconds (x weeks, x days, x hours), if its over a month then return (x months, x weeks, x days)

Posted: Mon Dec 27, 2004 2:27 pm
by protokol
Everything you could ever want:

http://www.php.net/datetime

Posted: Mon Dec 27, 2004 3:01 pm
by josh
Actually I always check the manual before posting, the only function that looks like it could work is strptime
Note: This function is not implemented on Windows platforms.
(my test server is on windows, forgot to mention that lil detail)

so thats why i posted here is to see if there is a user defined function equivalent to that.

Posted: Mon Dec 27, 2004 4:56 pm
by Robert Plank
I have a function that I wrote for a script of mine that does this exactly, you can use it as long as my name is left in the comments.

Code: Select all

<?php

/**
   Compares two Unix timestamps and outputs the result in a human-readable form.
   by Robert Plank -- http://www.jumpx.com

   @param $time1 the first timestamp
   @param $time2 the second timestamp
   @param $precision how detailed the comparison should be
          a precision of 2 might yield: "1 year, 1 month" or "2 weeks, 4 days"
          a precision of 4 might yield: "1 year, 1 month, 6 days, 5 hours"
          a precision of 0 will be as detailed as possible
   @param separator the token to separate units with (space, comma), etc.
          comment by default

   Examples:
    echo dateToText(strtotime("now"), strtotime("December 18th, 2004 02:56 PM PDT"), 0);
    echo dateToText(strtotime("now"), strtotime("December 18th, 2004 02:56 PM PDT"), 2, " ");
    echo dateToText(120);
*/

function dateToText($time1, $time2=0, $precision=2, $separator=', ') {

      $difference = abs($time1 - $time2);

      // Named unit and the number of seconds it takes up
      $units = array(
         "second" => 1,
         "minute" => 60,
         "hour" => 3600,
         "day" => 86400,
         "week" => 604800,
         "month" => 2592000,
         "year" => 31536000
      );

      $output = "";
      $i = 0;

      while (true) {
         reset($units);

         if ($difference >= end($units)) {
            end($units);
         }

         else {
            reset($units);
            while ($difference >= next($units)) ;
            prev($units);
         }

         list($unit, $value) = each($units);

         $magnitude = floor($difference / $value);

         $saveUnit = ($magnitude == 1) ? $unit : $unit."s";
         $output[] = array($magnitude, $saveUnit);

         $difference = $difference % $value;

         if ($precision > 0 && count($output) >= $precision || $difference == 0) {
            break;
         }
      }

      foreach ($output as $key => $value) {
         if ($value > 0) $output[$key] = implode(' ', $value);
      }
      return implode($separator, $output);
   }

?>
You can either put in two timestamps or just do dateToText(120) and get 2 minutes. Ignores 0 values, like if there are 0 hours. Oh yeah and you can edit the units like take out weeks if you want to by editing that array.

Posted: Tue Dec 28, 2004 4:45 am
by timvw
the thing i don't like about functions like these, is that they forget that the number of days in a month change... and thus return bogus results.

Posted: Tue Dec 28, 2004 5:31 am
by Robert Plank
I agree but if you're just looking at stuff like the time since the last login it's not really an issue. You won't be doing any arithmetic with it. Doing it the other way you'd need to deal with DST which you can't accurately do without timezones. You'd have to deal with leap years too. :P