tags:

views:

48

answers:

5
if($timestamp > time() - 60){ // Count seconds ago
$deff = time() - $timestamp;
$seconds = $deff / 60;
echo intval($seconds)." seconds ago";
}elseif($timestamp > time() - 3600){ // Count minutes ago
$deff = time() - $timestamp;
$minuts = $deff / 60;
echo intval($minuts)." minutes ago";
}

I think the minutes ago is right, but not the seconds? How should i do the "seconds ago" right?

A: 

I think your '$seconds' should just be equal to '$deff', shouldn't it?

Narcissus
A: 

DateInterval::format() provides a better approach.

http://php.net/manual/en/dateinterval.format.php

stillstanding
+3  A: 

Don't divide $deff by 60. The value is already represented in seconds, you don't need to convert it into seconds.

That code is a bit of a mess. Consider using the DateTime package; it has diffing and formatting functions built in.

Brock Batsell
A: 

I use this... it handles past and future times.

/**
 * Get a string representing the duration difference in two timestamps
 * 
 * @param   int   $time   The timestamp to compare
 * @param   int   $timeBase   The base timestamp to compare against, defaults to time()
 * @return  string   Returns the duration summary
 */
public function getTimeSummary ($time, $timeBase = false) {
    if (!$timeBase) {
        $timeBase = time();
    }

    if ($time <= time()) {
        $dif = $timeBase - $time;

        if ($dif < 60) {
            if ($dif < 2) {
                return "1 second ago";
            }

            return $dif." seconds ago";
        }

        if ($dif < 3600) {
            if (floor($dif / 60) < 2) {
                return "A minute ago";
            }

            return floor($dif / 60)." minutes ago";
        }

        if (date("d n Y", $timeBase) == date("d n Y", $time)) {
            return "Today, ".date("g:i A", $time);
        }

        if (date("n Y", $timeBase) == date("n Y", $time) && date("d", $timeBase) - date("d", $time) == 1) {
            return "Yesterday, ".date("g:i A", $time);
        }

        if (date("Y", $time) == date("Y", time())) {
            return date("F, jS g:i A", $time);
        }
    } else {
        $dif = $time - $timeBase;

        if ($dif < 60) {
            if ($dif < 2) {
                return "1 second";
            }

            return $dif." seconds";
        }

        if ($dif < 3600) {
            if (floor($dif / 60) < 2) {
                return "Less than a minute";
            }

            return floor($dif / 60)." minutes";
        }

        if (date("d n Y", ($timeBase + 86400)) == date("d n Y", ($time))) {
            return "Tomorrow, at ".date("g:i A", $time);
        }
    }

    return date("F, jS g:i A Y", $time);
}
Webnet
A: 

I use a small function similar to this, probably not the best and doesn't handle future times, but it's fine if all you want is the past.

function ago($when) {
        $diff = date("U") - $when;

        // Days
        $day = floor($diff / 86400);
        $diff = $diff - ($day * 86400);

        // Hours
        $hrs = floor($diff / 3600);
        $diff = $diff - ($hrs * 3600);

        // Mins
        $min = floor($diff / 60);
        $diff = $diff - ($min * 60);

        // Secs
        $sec = $diff;

        // Return how long ago this was. eg: 3d 17h 4m 18s ago
        // Skips left fields if they aren't necessary, eg. 16h 0m 27s ago / 10m 7s ago
        $str = sprintf("%s%s%s%s",
                $day != 0 ? $day."d " : "",
                ($day != 0 || $hrs != 0) ? $hrs."h " : "",
                ($day != 0 || $hrs != 0 || $min != 0) ? $min."m " : "",
                $sec."s ago"
        );

        return $str;
}

Pass a unix timestamp to it (your time in the past) and it will say how long ago it was. Easily modifiable if you want the return value to be slightly different, or perhaps want to add number of months or years into it.

signine