How to Implement a Facebook-like 'time ago' Function

Learn how to implement a facebook-like "time ago" function by using the PHP unix timestamp.

In most social networks today, a "time ago" time format is currently being used, instead of the exact date of the post. This format helps the users to get the information without dealing with any time format and timezone. The code to write the function is below:
function getTimeAgo($sTimeStamp)
{
$sTimeAgo = strtotime($sTimeStamp); // The "strtotime" function converts the date and time to unix timestamp.
$sCurrentTime = time(); // Returns the current date and time in unix timestamp.
$sDifference = ($sCurrentTime - $sTimeAgo); // The difference gives the number of seconds
// Number of Seconds
$sSeconds = $sDifference;
// Number of Minutes
$sMinutes = round($sSeconds/60); // Value of 60 is the number of seconds in 1 minute
// Number of Hours
$sHour = round($sSeconds/3600); // Value of 3600 is the number of seconds in 1 hour. (60 seconds * 60 minutes)
// Number of Days
$sDays = round($sSeconds/86400); // Value of 86400 is the number of seconds in 1 day. (60 seconds * 60 minutes * 24 hours)
// Number of Weeks
$sWeeks = round($sSeconds/604800); // Value of 86400 is the number of seconds in 1 week. (60 seconds * 60 minutes * 24 hours * 7 days)
// Number of Months
$sMonths = round($sSeconds/2629440); // Value of 2629440 is the number of seconds in 1 month. (60 seconds * 60 minutes * 24 hours * ((365 * 4)+366/5/12))
// Number of Years
$sYear = round($sSeconds/31553280); // Value of 31553280 is 2629440 * 12
if($sSeconds < 60)
{
return 'Just Now';
}
elseif($sMinutes < 60)
{
if($sMinutes == 1)
{
return "a min ago";
}
else
{
return "$sMinutes min ago";
}
}
elseif($sHour < 24)
{
if($sHour == 1)
{
return "an hour ago";
}
else
{
return "$sHour hrs ago";
}
}
elseif($sWeeks <= 4.3) //4.3 == 52/12
{
if($sWeeks == 1)
{
return "a week ago";
}
else
{
return "$sWeeks weeks ago";
}
}
elseif($sMonths <= 12)
{
if($sMonths == 1)
{
return "a month ago";
}
else
{
return "$sMonths month ago";
}
}
else
{
if($sYear == 1)
{
return "one year ago";
}
else
{
return "$sYear year ago";
}
}
}