Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display time in x days ago in php [closed]

Tags:

php

datetime

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.

like image 955
Ansha Avatar asked Oct 17 '25 08:10

Ansha


2 Answers

// 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 >= 120) {
    echo "Seen " . intval($seconds_ago / 60) . " minutes ago";
} elseif ($seconds_ago >= 60) {
    echo "Seen a minute ago";
} else {
    echo "Seen less than a minute ago";
}
like image 195
MrCarrot Avatar answered Oct 19 '25 21:10

MrCarrot


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

like image 45
Suchit kumar Avatar answered Oct 19 '25 21:10

Suchit kumar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!