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
Don Jazzy Kneels Down To Greet Veteran Actor, Kanayo. O. Kanayo As They Meet For The First Time (Video)
Forum

Don Jazzy Kneels Down To Greet Veteran Actor, Kanayo. O. Kanayo As They Meet For The First Time (Video)

2 days ago
BBNaija’s Mike’s Wife, Perri Tries Out Garri For The First Time (Video)
Forum

BBNaija’s Mike’s Wife, Perri Tries Out Garri For The First Time (Video)

1 week ago
Comedian, Princess, says her foster child is the victim actor Baba Ijesha allegedly defiled 7 years ago (video)
Forum

Comedian, Princess, says her foster child is the victim actor Baba Ijesha allegedly defiled 7 years ago (video)

2 weeks ago
Man United and Chelsea on red alert as Real Madrid star tells friends his time with club is over
Forum

Man United and Chelsea on red alert as Real Madrid star tells friends his time with club is over

2 weeks ago