My Programming Tutorials

My Programming Tutorials

Facebook like time ago system in php

Last updated on by , 7 comments

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 to convert 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 an 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 is 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 the current date and time & previous date and time with simple PHP logic. This function converts the previous date into UNIX timestamp and also the current date in UNIX time. From the difference of this current UNIX timestamp and previous UNIX timestamp, we can get the 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 your 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

Author Info

Paritosh Pandey

He loves Technology

Advertisement

7 responses to “Facebook like time ago system in php”

  1. Shakti Singh says:

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

  2. ScalperBTC says:

    How to get just such a publication date: 01:17 minutes ago?

    Thank you in advance for your response!

  3. Abdoulanziz Ally says:

    This has been so helpful to me…Lot’s of thanks!

  4. Prashant says:

    this solution works, though it would not be very efficient to write so many if-else

  5. Davis says:

    This has really helped me. Thanks a lot. However, i want you to help me show it from database

  6. Bruno G. says:

    if ($weeks <= 4.3) is strange, since $weeks was rounded when calculated. Maybe it should only be rounded when value is used in the return?

Leave a Reply

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