XX Time Ago PHP function.

6 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
Forum

My dad is the greatest of all time – Wizkid’s son, Boluwatife

4 days ago
Forum

Mandalorian: Katee Sackhoff Teared Up First Time She Wore Bo-Katan Armor

6 days ago
Forum

This Video Of 2Face And Wife Annie Idibia Having A Romantic Time Is Just Lovely And Cute

2 weeks ago
Forum

“Wathoni Is So Pretty, She’s Fine” – Mercy Eke Hails Wathoni As They Meet For The First Time (Watch Video)

2 weeks ago