The “time ago” problem and how I got there

Looking at apps and websites me and my group got our inspiration from there was one thing I realized that we needed so our users could improve their experience using our page: a timestamp for the posts. Imagine accessing the website, looking at the “posting-page” and realizing that you have no idea how old those posts are. They could have been written weeks or even a month ago. Since our whole concept is based around the idea of anonymity and withholding information, we also have to make sure that we do not cut the user off too much. There are a couple of things the user needs to identify with the poster. One of them is the gender, a part we have already integrated. The two other things that were still missing were a place and time. The first thing that had to be done to even think about integrating time was updating the table in the database. Our “posts-table” needed a TIMESTAMP. Since I had already set up one for our “users-table”, it was not very hard to do. A couple of settings had to be adjusted. As you can see in the following photo, I set the TYPE to “timestamp” as well as the DEFAULT to “CURRENT_TIMESTAMP”. Without any extra requirements from the user, every post would automatically be saved with a timestamp.

The easiest option at this point would have been to just echo out the time-stamp with our posts and let the user do the math. But since we wanted to create a user-friendly, easy to use site, I started looking into using the current time and getting the difference between it and the timestamp time. Since I only had a vague idea of how to do that, I was faced with having to do quite a lot of research.

Before I started doing the research, I wrote down a list of things I needed to get more knowledge about:

  1. How to get the current time?
  2. How to get the difference between the current time and the timestamp?
  3. How to convert the whole thing in the right format (“..minutes ago”/”..hours ago”/etc.)?

First, I looked into how to get the current time:

  • For that, I looked at a tutorial from To my surprise, it was actually not very hard to get the current time. All it needed was PHP’s date() function. Then, I even had a free choice on how the format was going to be. In the following you can see an extract from the tutorial. First the characters needed for dates, then for times:

Screenshot (280) Screenshot (281)

Secondly, I looked into getting the difference between the current time and the timestamp:

To do so, I looked at the website, where I found out that I could use one of PHP’s functions called mktime() to get what I wanted. The function would give out the different between two timestamps in seconds. According to the tutorial I looked at, it was the easiest to “divide the difference in second in different values and taking the floor math value of it.” In the following, you can see an extract from the website, explaining the mathematical principles behind it:

To get the difference in minutes we have to divide by 60

To get the difference in hours we have to divide by 3600 ( 60 minute x 60 seconds )

To get the difference tin days we have to divide by 86400 ( 24 x 60 x 60 )

To get the difference in months we have to divide by 2628000

Also, here you can see a picture of the very first and basic code, I ended up having. Part of it is an extract from the tutorial, part of it, I added from what I had learned.  As you can see, I set the default time zone to “UTC” because my German-Laptop ended up giving me wrong current time. Without it, I would have never noticed. Also, for the current time, I used the format “H:i:s”. Looking at the previously referenced explanations from w3schools, it means a result like “12:12:12”.

Screenshot (275)

Echoed out, my code looked like this (you can see the differences as well as the current timestamp at the end):Screenshot (273)

Thirdly, I looked into the format:

This was the part, where I got stuck. I wanted the posts to have a nice little “..ago” field with information and not a whole block of confusing numbers and dates. I looked into this and after hours of trying around, I ended up at the following tutorial:

It explained the whole thing from the very beginning, so I could freshen up everything I had learned and because it gave a lot of additional ideas and suggestions, I ended up redoing my code from the very beginning. The tutorial ended up writing two functions that then easily could be used to echo out the time in my wanted format. I followed it and ended up with two functions in my “functions.php” file. Because I knew that I would reference the tutorial in my code, I stuck to the function names the tutorial suggested and just changed some small things while writing the code. One of them was adding the default time zone again, for example. If you look at the functions, you can see that the first one converts into a timestamp in the “2015-01-01 12:12:12” format and the second one gives out the differences between the time and the timestamp-time.


class convertToAgo {

 function convert_datetime($str) {
 //sets the timezone to UTC
 list($date, $time) = explode(' ', $str);
 list($year, $month, $day) = explode('-', $date);
 list($hour, $minute, $second) = explode(':', $time);
 $timestamp = mktime($hour, $minute, $second, $month, $day, $year);
 return $timestamp;

 function makeAgo($timestamp){
 $difference = time() - $timestamp;
 $periods = array("sec", "min", "hr", "day", "week", "month", "year", "decade");
 $lengths = array("60","60","24","7","4.35","12","10");
 for($x = 0; $difference >= $lengths[$x]; $x++){
 $difference /= $lengths[$x];
 $difference = round($difference);
 if($difference != 1) $periods[$x].= "s";
 $text = "$difference $periods[$x] ago";
 return $text;



 <div class="time_ago" <h2><?php echo "..".$when; ?></h2> </div>

And here you can see the actual result in the top left corner:

Screenshot (284)


W3Schools, unknown. PHP 5 Date and Time. Available from: [Accessed 17th March 2015].

Plus2net, unknown. PHP Date Difference calculation using mktime function. Available from: [Accessed 17th March 2015].

DevelopPHP, unknown. Convert MySQL Timestamp to Ago Time Format OOP Tutorial. Available from: [Accessed 18th March 2015].