Facebook like time ago system in php

Last updated on August 31st, 2017 by Paritosh Pandey


In this tutorial we are going to learn how to make Facebook like time ago system in PHP. In my previous post we have learnt How to close a DIV container when clicking outside using Jquery, I know it’s kinda off topic, because in this topic we are going to learn converting timestamp to time ago in php, but it is my duty to expose all those interesting articles so that you can learn more.

Guys If you have used Facebook and you update status on your wall, upload photo or share something on Facebook then after uploading it, It display time as Just Now or you can also see your previous post time, it show like one week or month or year or day ago.

facebook like time ago system in php

Importance of this System (Facebook like time ago system in php)

It is common to show dates on the Web in the format such as Published on January 12th 2017, or 05/17/2017, 09:40:25 and 2017-09-12.

Each of the above examples shows the full date or time of some kind of activity on web applications – be it a published blog article, or a visitor comment, or perhaps an uploaded video.

Showing Date formats like above might seem perfectly reasonable. After all, they are informative and human readable! but “human readable” not at all necessary mean readers will readily be able to understand how recently the activity has occurred or added. The Internet is a fast moving place, and giving your content a sense of freshness could be the key to engaging with your audience.

Let’s suppose you have stumbled on a article that was published just a couple of hours ago, but the subheading of the article states like:

Published on January 12th 2017

or

05/17/2017

or

09:40:25

or

2017-09-12

The only problem with above messages is that they do not communicate the feeling that your activity has just been updated. So, surely it be more inviting and much clearer to display the time in this way:

Published 2 hours ago

Strategy And Logics

This function counts the time between current date and time & previous date and time with simple php logic. This function converts previous date into UNIX timestamp and also current date in UNIX time. From difference of this current UNIX time stamp and previous UNIX timestamp we can get number of seconds between these dates. From seconds we can easily get minutes, hours, days, weeks, months and years.

Time ago in php function


function time_ago_in_php($timestamp){
  
  date_default_timezone_set("Asia/Kolkata");         
  $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){

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

    }
  }
}

You just have to call the function like this:

echo time_ago_in_php(‘2017-06-16 10:57:03’);

So! this is all in facebook like time ago system in php module, to learn more programming tricks please share this with you friends, and don’t forget to subscribe our blog because as I said before we are going to post lots of interesting programming tutorials in future, stay tuned.

You may also like

One response to “Facebook like time ago system in php”

  1. Hii Very Good Article .
    Thanking For Sharing Keep Up The Good Work

Leave a Reply

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