Question

Could you please help me.

I have a unix timestamp that i want to convert to a last seen function

Example:

current unix timestamp - last seen timestamp = 2 days, 19 hours, 05 min and 81 sec

Was it helpful?

Solution 2

Hi i found a solution that is must faster and easy to use. Tanx for the reply

function ago($unix)
{
    $datetime1 = new DateTime(date("Y-m-d H:i:s", $unix));
    $datetime2 = new DateTime();
    $interval = $datetime1->diff($datetime2);
    return $interval->format('%a days, %H hours, %I min and %S sec  ');
}

This returns 0 days, 19 hours, 33 min and 31 sec

OTHER TIPS

Try this ,

function ago($mtime)
{
$xtime = time() - $mtime;

if ($xtime < 1)
{
    return '0 seconds';
}

$a = array( 12 * 30 * 24 * 60 * 60  =>  'year',
            30 * 24 * 60 * 60       =>  'month',
            24 * 60 * 60            =>  'day',
            60 * 60                 =>  'hour',
            60                      =>  'minute',
            1                       =>  'second'
            );

foreach ($a as $secs => $str)
{
    $d = $xtime / $secs;
    if ($d >= 1)
    {
        $r = round($d);
        return $r . ' ' . $str . ($r > 1 ? 's' : '') . ' ago';
    }
}}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top