XX Time Ago PHP function.

7 years ago

Use this simple PHP function to show time of actions as either x seconds ago, x minutes ago, x hours ago, x days ago, x months ago or x years ago.

Code:

function timeago($time){
$time_difference = time() - $time;
$seconds = $time_difference ;
$minutes = round($time_difference / 60);
$hours = round($time_difference / 3600);
$days = round($time_difference / 86400);
$weeks = round($time_difference / 604800);
$months = round($time_difference / 2419200);
$years = round($time_difference / 29030400);

if($seconds <= 60)
{
Return "$seconds seconds ago";
}
elseif($minutes <= 60)
{
if($minutes==1)
{ Return"one minute ago"; }
else
{ Return"$minutes minutes ago"; }
}
elseif($hours <= 24)
{
if($hours==1)
{ Return"one hour ago"; }
else
{ Return"$hours hours ago"; }
}
elseif($days <=7)
{
if($days==1)
{ Return"one day ago"; }
else
{ Return"$days days ago"; }
}
elseif($weeks <=4)
{
if($weeks==1)
{ Return"one week ago"; }
else
{ Return"$weeks weeks ago"; }
}
elseif($months <=12)
{
if($months==1)
{ Return"one month ago"; }
else
{ Return"$months months ago"; }
}
else
{
if($years==1)
{ Return"one year ago"; }
else
{ Return"$years years ago"; }
}
}

Related forums
Let It Be The Last Time - Tems
Forum

Let It Be The Last Time - Tems' Diehard Fan Gives Wizkid Stern Warning Over Viral Video

13 hours ago
Chelsea boss Tuchel: I was involved in Saul signing; he still needs time to adapt
Forum

Chelsea boss Tuchel: I was involved in Saul signing; he still needs time to adapt

1 day ago
Excitement As Peter And Paul Of P-Square Perform Together For The First Time Since Their Reunion
Forum

Excitement As Peter And Paul Of P-Square Perform Together For The First Time Since Their Reunion

4 days ago
Davido Neglected Me In My Time Of Need But Donated N250m To Orphanages — Baba Fryo Blows Hot
Forum

Davido Neglected Me In My Time Of Need But Donated N250m To Orphanages — Baba Fryo Blows Hot

1 week ago