This website uses cookies in order to offer you the most relevant information and services online. Please accept cookies for optimal performance.
Learn More

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

Apr 20, 2020

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

facebook-like-time-ago-function

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";
        }
    }
}

PHP Application Framework
Batoi Framework is a PHP application framework with an Integrated Development Environment for Next Generation Apps and Services on the Cloud.
Create your web application without worrying about small things.

Need our assistance? We are available with you.

Existing Customer?
Get Support
Talk To Consultant?
Get An Appointment
Contact Us

Batoi global offices and contact details

Stay Connected

Join our community, newsletters, and social media.

Batoi's new website is in the process of release and will be completed by the end of October 2020. Please bear with us.