2

I want to show last seen in user's profile in my php page. I am storing user's logout time in database as 2014-01-06 15:25:08 (store in $last_log) with DATETIME datatype. Now i want to display last seen x mins ago. And it's auto update in x day ago, x month ago.

I want same as here when we add comment & its time ".......ago" updates. How can i display this.

Ansha
  • 55
  • 1
  • 1
  • 7
  • possible duplicate of [convert time stamp to time ago in php?](http://stackoverflow.com/questions/26225486/convert-time-stamp-to-time-ago-in-php) – worldofjr Dec 06 '14 at 10:26
  • 2
    What did you tried ? – mpromonet Dec 06 '14 at 10:32
  • Thanks worldofjr, Its working when i edit in database, But when i logout now then check after again login, It is displaying 5 hours ago. I can't understand why? – Ansha Dec 06 '14 at 10:42
  • possible duplicate of [Converting timestamp to time ago in PHP e.g 1 day ago, 2 days ago...](http://stackoverflow.com/questions/1416697/converting-timestamp-to-time-ago-in-php-e-g-1-day-ago-2-days-ago) – Glavić Dec 06 '14 at 16:18

5 Answers5

14
// intval() - http://php.net/manual/en/function.intval.php

$seconds_ago = (time() - strtotime('2014-01-06 15:25:08'));

if ($seconds_ago >= 31536000) {
    echo "Seen " . intval($seconds_ago / 31536000) . " years ago";
} elseif ($seconds_ago >= 2419200) {
    echo "Seen " . intval($seconds_ago / 2419200) . " months ago";
} elseif ($seconds_ago >= 86400) {
    echo "Seen " . intval($seconds_ago / 86400) . " days ago";
} elseif ($seconds_ago >= 3600) {
    echo "Seen " . intval($seconds_ago / 3600) . " hours ago";
} elseif ($seconds_ago >= 60) {
    echo "Seen " . intval($seconds_ago / 60) . " minutes ago";
} else {
    echo "Seen less than a minute ago";
}
MrCarrot
  • 2,546
  • 1
  • 23
  • 29
5

try something like this:

$datetime1 = new DateTime('2014-01-06 15:25:08');
$datetime2 = new DateTime();
$interval = $datetime1->diff($datetime2);
echo $interval->format('%a days')."<br>";
  echo $interval->m." Months";

for more read this:http://php.net/manual/en/datetime.diff.php

Suchit kumar
  • 11,809
  • 3
  • 22
  • 44
5

Try this function

function get_timeago( $ptime )
{
$etime = time() - $ptime;

if( $etime < 1 )
{
    return 'less than '.$etime.' second ago';
}

$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 = $etime / $secs;

    if( $d >= 1 )
    {
        $r = round( $d );
        return 'about ' . $r . ' ' . $str . ( $r > 1 ? 's' : '' ) . ' ago';
    }
}
}

Usage :

$timestamp = strtotime("2014-11-14 17:15:59");
echo get_timeago( $timestamp );
Arun Kumar
  • 1,607
  • 1
  • 18
  • 33
  • it's return "less than 1 minute ago" every time. please solve this – Ansha Dec 17 '14 at 06:52
  • mins not auto update. want same as here displaying in above comment...mins ago. & it is alwz updates – Ansha Dec 17 '14 at 07:07
  • Check Your default time zone . it's working fine with minute – Arun Kumar Dec 17 '14 at 07:21
  • if you want seconds use this code return 'less than '.$etime.' seconds ago'; – Arun Kumar Dec 17 '14 at 07:27
  • not only with seconds. it returns "-11080 minute ago " I want it as 1 mins ago, 5 mins ago, 2 hours ago, 3 days ago....auto update. Please read again my question – Ansha Dec 17 '14 at 07:47
  • In this what is $ptime. in my database 2014-11-14 17:15:59 with datetime datatype, which is storing $ last_login. Pleas egive some solution. i can't access it. – Ansha Dec 17 '14 at 13:11
  • Call get_timeago(YOUR_VALUE) function. Ex: get_timeago($last_login); – Arun Kumar Dec 18 '14 at 05:39
  • Hi Aurn, When i use this function like "echo get_timeago( $last_log );" it return '46 years ago '. And when i use like "$last_log = strtotime($last_log); echo get_timeago( $last_log );" then return ' less than -19164 second ago'. Here I want "3 mins ago". Where the mistake. :( – Ansha Dec 18 '14 at 06:44
  • kindly send the $last_log value – Arun Kumar Dec 18 '14 at 07:26
  • @Ansha I guess u r getting minus Values. That means U maybe tested with bigger time than current time – Rakesh Jan 04 '15 at 15:00
3

Use the date_diff function.

See http://php.net/manual/en/function.date-diff.php

"Powerful Function to get two date difference."

In short:

// $datetime1 and $datetime2 are UNIX timestamps.
$interval = date_diff($datetime1, $datetime2);
echo $interval->format($differenceFormat);
Franz Holzinger
  • 913
  • 10
  • 20
3

try this

$date1 = strtotime('2014-12-06 15:25:08');
$date2 = strtotime(date('Y-m-d H:i:s'));
$seconds_diff = $date2 - $date1;

echo round(abs($seconds_diff) / 60,2). " mins ago";
manuyd
  • 201
  • 1
  • 8