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

Fast & Furious: Why Time Travel Is Perfect For The Two-Part Ending

5 days ago
Forum

#EndSARS: ‘This Is Not The Time To Post Beautiful Photos & Videos’ – Tacha

2 weeks ago
Forum

No Time To Die Director Teams Up With Steven Spielberg For Apple TV+ Series

2 weeks ago
Forum

“You Think They Had Some Time To Discuss SARS?” – Wizkid Reacts To Buhari Meeting With The National Security Adviser

2 weeks ago