Facebook Style time ago function using PHP

In this tutorial you’ll learn How to get Facebook, Instagram, WhatsApp and so many social media platform style time ago function in PHP. Friends When you use Facebook and update a status on your wall or upload a photo to your Facebook wall, after uploading, the time will be shown as Now or you can also see the time of the previous post, it is the week, month , year or day. In this tutorial we will learn how to save this time using the PHP programming language.

This is a very simple PHP script to get output like Facebook style some time ago. In this function we can calculate the time based on the time zone of a specific country.

If you have a previous date and time, this function will count the time between the current date and time and the previous date using simple PHP logic.

In this function I convert the previous date and time to a UNIX timestamp and the current date and time also get the UNIX time. From the difference between the current and previous UNIX timestamps, we can find the number of seconds between this date and time. From seconds we can easily determine minutes, hours, days, weeks, months and years. In this post I also share the source code of this post.

see also

  1. Make Instagram clone using HTML CSS and JavaScript
  2. Build Instagram like App in HTML CSS and JavaScript
  3. Create Instagram Story views using JavaScript

Time Ago Function in PHP

<?php

date_default_timezone_set("Asia/Calcutta");   //India time (GMT+5:30) 

function facebookTimeAgo($timestamp)
{
    $time_ago = strtotime($timestamp);
    $current_time = time();
    $time_difference = $current_time - $time_ago;
    $seconds    = $time_difference;
    $minutes    = round($seconds / 60);           // value 60 is seconds
    $hours      = round($seconds / 3600);           //value 3600 is 60 minutes * 60 sec
    $days       = round($seconds / 86400);          //86400 = 24 * 60 * 60;
    $weeks      = round($seconds / 604800);          // 7*24*60*60;
    $months     = round($seconds / 2629440);     //((365+365+365+365+366)/5/12)*24*60*60
    $years      = round($seconds / 31553280);     //(365+365+365+365+366)/5 * 24 * 60 * 60

    if ($seconds <= 60) {
        return "Just Now";
    } else if ($minutes <= 60) {

        if ($minutes == 1) {
            return "one minute ago";
        } else {
            return "$minutes minutes ago";
        }

    } else if ($hours <= 24) {
        if ($hours == 1) {
            return "an hour ago";
        } else {
            return "$hours hrs ago";
        }
    } else if ($days <= 7) {
        if ($days == 1) {
            return "yesterday";
        } else {
            return "$days days ago";
        }
    } else if ($weeks <= 4.3) //4.3 == 52/12
    {
    if ($weeks == 1) {
            return "a week ago";
        } else {
            return "$weeks weeks ago";
        }
    } else if ($months <= 12) {
        if ($months == 1) {
            return "a month ago";
        } else {
            return "$months months ago";
        }
    } else {
        if ($years == 1) {
            return "one year ago";
        } else {
            return "$years years ago";
        }
    }
}
?>

Create HTML Page to Design a Sample Post

In this post we will create a sample static post page using bootstrap with CDN (Content Delivery Network) links.

index.php

<!DOCTYPE html>
<html lang="">
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <title>Facebook style time ago function using PHP</title>
        <!-- Bootstrap CSS -->
        <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet">
    </head>
<body>
    <div class="container mt-5 mb-5">
    <div class="row d-flex align-items-center justify-content-center">
        <div class="col-md-6">
            <div class="card">
                <div class="d-flex justify-content-between p-2 px-3">
                    <div class="d-flex flex-row align-items-center"> <img src="https://i.imgur.com/UXdKE3o.jpg" width="50" class="rounded-circle">
                        <div class="d-flex flex-column ml-2">
                         <span class="font-weight-bold">Jeanette Sun</span> 
                            <small class="text-primary">Collegues</small> 
                        </div>
                    </div>
                    <div class="d-flex flex-row mt-1 ellipsis"> <small class="mr-2"><?php echo facebookTimeAgo('2023-10-07 04:58:00');  ?></small> <i class="fa fa-ellipsis-h"></i> </div>
                </div> 
                <img src="https://i.imgur.com/xhzhaGA.jpg" class="img-fluid">
                <div class="p-2">
                    <p class="text-justify">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt.</p>
                    <hr>
                    <div class="d-flex justify-content-between align-items-center">
                        <div class="d-flex flex-row icons d-flex align-items-center"> <i class="fa fa-heart"></i> <i class="fa fa-smile-o ml-2"></i> </div>
                        <div class="d-flex flex-row muted-color"> <span>2 comments</span> <span class="ml-2">Share</span></div>
                    </div>
                    <hr>
                    <div class="comments">
                        <div class="d-flex flex-row mb-2"> <img src="https://i.imgur.com/9AZ2QX1.jpg" width="40" class="rounded-image">
                            <div class="d-flex flex-column ml-2"> <span class="name">Daniel Frozer</span> <small class="comment-text">I like this alot! thanks alot</small>
                                <div class="d-flex flex-row align-items-center status"> <small>Like</small> <small>Reply</small> <small>Translate</small> <small>18 mins</small> </div>
                            </div>
                        </div>
                        <div class="d-flex flex-row mb-2"> <img src="https://i.imgur.com/1YrCKa1.jpg" width="40" class="rounded-image">
                            <div class="d-flex flex-column ml-2"> <span class="name">Elizabeth goodmen</span> <small class="comment-text">Thanks for sharing!</small>
                                <div class="d-flex flex-row align-items-center status"> <small>Like</small> <small>Reply</small> <small>Translate</small> <small>8 mins</small> </div>
                            </div>
                        </div>
                        <div class="comment-input"> <input type="text" class="form-control">
                            <div class="fonts"> <i class="fa fa-camera"></i> </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
</body>
</html>


<?php

date_default_timezone_set("Asia/Calcutta");   //India time (GMT+5:30) 

function facebookTimeAgo($timestamp)
{
    $time_ago = strtotime($timestamp);
    $current_time = time();
    $time_difference = $current_time - $time_ago;
    $seconds    = $time_difference;
    $minutes    = round($seconds / 60);           // value 60 is seconds
    $hours      = round($seconds / 3600);           //value 3600 is 60 minutes * 60 sec
    $days       = round($seconds / 86400);          //86400 = 24 * 60 * 60;
    $weeks      = round($seconds / 604800);          // 7*24*60*60;
    $months     = round($seconds / 2629440);     //((365+365+365+365+366)/5/12)*24*60*60
    $years      = round($seconds / 31553280);     //(365+365+365+365+366)/5 * 24 * 60 * 60

    if ($seconds <= 60) {
        return "Just Now";
    } else if ($minutes <= 60) {

        if ($minutes == 1) {
            return "one minute ago";
        } else {
            return "$minutes minutes ago";
        }

    } else if ($hours <= 24) {
        if ($hours == 1) {
            return "an hour ago";
        } else {
            return "$hours hrs ago";
        }
    } else if ($days <= 7) {
        if ($days == 1) {
            return "yesterday";
        } else {
            return "$days days ago";
        }
    } else if ($weeks <= 4.3) //4.3 == 52/12
    {
    if ($weeks == 1) {
            return "a week ago";
        } else {
            return "$weeks weeks ago";
        }
    } else if ($months <= 12) {
        if ($months == 1) {
            return "a month ago";
        } else {
            return "$months months ago";
        }
    } else {
        if ($years == 1) {
            return "one year ago";
        } else {
            return "$years years ago";
        }
    }
}

?>
Facebook Style time ago function using PHP

You can always support by sharing on social media or recommending my blog to your friends and colleagues.  If you have any suggestions or problems about this tutorial, please comment on the  form below.😊

Leave a Reply

Your email address will not be published. Required fields are marked *