3

Hello in my database date / time are in this format

2010-06-01T18:20:25+0000

I'd like to echo that out to time passed since that date / time

e.g.

4 days 3 hours 36 minutes and 4 seconds

is this possible?

Webby
  • 2,655
  • 5
  • 26
  • 34

5 Answers5

4

Below is a function I wrote to do this. Feel free to use it.

/**
 * Returns rough (in largest single unit) time elapsed between two times.
 * @param int $iTime0  Initial time, as time_t.
 * @param int $iTime1  Final time, as time_t. 0=use current time.
 * @return string Time elapsed, like "5 minutes" or "3 days" or "1 month".
 *              You might print "ago" after this return if $iTime1 is now.
 * @author Dan Kamins - dos at axonchisel dot net
 */
function ax_getRoughTimeElapsedAsText($iTime0, $iTime1 = 0)
{
    if ($iTime1 == 0) { $iTime1 = time(); }
    $iTimeElapsed = $iTime1 - $iTime0;

    if ($iTimeElapsed < (60)) {
        $iNum = intval($iTimeElapsed); $sUnit = "second";
    } else if ($iTimeElapsed < (60*60)) {
        $iNum = intval($iTimeElapsed / 60); $sUnit = "minute";
    } else if ($iTimeElapsed < (24*60*60)) {
        $iNum = intval($iTimeElapsed / (60*60)); $sUnit = "hour";
    } else if ($iTimeElapsed < (30*24*60*60)) {
        $iNum = intval($iTimeElapsed / (24*60*60)); $sUnit = "day";
    } else if ($iTimeElapsed < (365*24*60*60)) {
        $iNum = intval($iTimeElapsed / (30*24*60*60)); $sUnit = "month";
    } else {
        $iNum = intval($iTimeElapsed / (365*24*60*60)); $sUnit = "year";
    }

    return $iNum . " " . $sUnit . (($iNum != 1) ? "s" : "");
}

To use this func, you'd need to first convert your times to time_t format (integer #seconds since the "epoch"). Either of these PHP functions will probably help with that: http://php.net/strptime or http://php.net/strtotime.

dkamins
  • 21,450
  • 7
  • 55
  • 59
  • Question was edited since I posted this... My function only outputs the largest time units, which is often all a user cares about, and what the original question seemed to be asking. – dkamins Jun 01 '10 at 18:55
  • Thanks buddy I managed to convert to timestamp like you said however the out put is just "7 days" it doesn't output minutes etc? – Webby Jun 01 '10 at 20:33
  • @Webby, as I said, it only outputs the largest time chunk. If the time is between 61 seconds and 59 minutes + 59 seconds, it will output "minutes" (and only minutes). – dkamins Jun 01 '10 at 23:01
  • I actually decided to go with your function in the end, like you said its all users care about.. my request was over kill haha thanks – Webby Jun 02 '10 at 10:45
  • Then use $granularity = 1 in my function. You like all those elseif() statements? – Entendu Jun 02 '10 at 16:31
2

I changed one line of the above code for the case in which it was posted less than one minute ago.

    function ax_getRoughTimeElapsedAsText($iTime0, $iTime1 = 0)
    {
    if ($iTime1 == 0) { $iTime1 = time(); }
    $iTimeElapsed = $iTime1 - $iTime0;

    if ($iTimeElapsed < (60)) {
        return "Less than a minute ago";
    } else if ($iTimeElapsed < (60*60)) {
        $iNum = intval($iTimeElapsed / 60); $sUnit = "minute";
    } else if ($iTimeElapsed < (24*60*60)) {
        $iNum = intval($iTimeElapsed / (60*60)); $sUnit = "hour";
    } else if ($iTimeElapsed < (30*24*60*60)) {
        $iNum = intval($iTimeElapsed / (24*60*60)); $sUnit = "day";
    } else if ($iTimeElapsed < (365*24*60*60)) {
        $iNum = intval($iTimeElapsed / (30*24*60*60)); $sUnit = "month";
    } else {
        $iNum = intval($iTimeElapsed / (365*24*60*60)); $sUnit = "year";
    }

    return $iNum . " " . $sUnit . (($iNum != 1) ? "s" : "") . " ago";
    }
1

you can use the func timediff right in your database:

http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_timediff

pass the first param as the date, and the second param as now()

Zak
  • 24,947
  • 11
  • 38
  • 68
1
function time_ago($timestamp, $granularity = 2) {
  $timestamp = time() - $timestamp;
  $units = array('1 year|%d years' => 31536000, 
                 '1 week|%d weeks' => 604800, 
                 '1 day|%d days' => 86400, 
                 '1 hour|%d hours' => 3600, 
                 '1 min|%d mins' => 60, 
                 '1 sec|%d secs' => 1
                );
  $output = '';
  foreach ($units as $key => $value) {
    $key = explode('|', $key);
    if ($timestamp >= $value) {
      $pluralized = floor($timestamp / $value) > 1 ? 
                    sprintf($key[1], floor($timestamp / $value)) : 
                    $key[0];
      $output .= ($output ? ' ' : '') . $pluralized;
      $timestamp %= $value;
      $granularity--;
    }
    if ($granularity == 0) {
      break;
    }
  }
  return $output ? $output : "Just now";
}

This should be close.

Edit: added this line: $timestamp = time() - $timestamp;

Entendu
  • 1,235
  • 10
  • 12
  • I can seem to get this function to work? im trying : $c = function time_ago("1274744330","2"); echo "$c
    "; am i wrong? many thanks
    – Webby Jun 01 '10 at 20:35
  • That is wrong. $c = time_ago("1274744330","2"); echo "$c
    "; (notice the lack of the "function" keyword?) I get: 40 years 21 weeks
    – Entendu Jun 01 '10 at 21:09
  • Yeah, realized 40 years was wrong. Forgot a calculation, the answer is edited. Now I'm getting 1 week 21 hours with granularity 2 and 1 week 21 hours 37 mins 24 secs with full granularity. – Entendu Jun 01 '10 at 21:18
-1

u can fetch that fron db and use strtotime function which will make into epoch . and then use date command to print it into any format u want to .

Hacker
  • 7,798
  • 19
  • 84
  • 154