Retrieve time period like facebook fashion


facebookIn facebook stream you’ll see the time period at the bottom of the stream. For example: 4 minutes ago, 2 days ago, 3 weeks ago…. In our recent project we have to show similar time fashion for our application’s activity stream. So I write a function to retrieve the time duration.

In our mysql database, we used a column named ‘created‘ as DATETIME in the table. I retrieve that created field as unix_timestamp. So the sql query looks like “SELECT UNIX_TIMESTAMP(created) as created from tableName

After getting the data I just pass the created value in my function. Here is the function:

/*
 * @method: getTimeDuration
 * @param: unix timestamp
 * @return: duration of minutes, hours, days, weeks, months, years
 */
 function getTimeDuration($unixTime) {
     $period    =   '';
     $secsago   =   time() - $unixTime;

     if ($secsago < 60){
         $period = $secsago == 1 ? '1 second'     : $secsago . ' seconds';
     }
     else if ($secsago < 3600) {
         $period    =   round($secsago/60);
         $period    =   $period == 1 ? '1 minute' : $period . ' minutes';
     }
     else if ($secsago < 86400) {
         $period    =   round($secsago/3600);
         $period    =   $period == 1 ? '1 hour'   : $period . ' hours';
     }
     else if ($secsago < 604800) {
         $period    =   round($secsago/86400);
         $period    =   $period == 1 ? '1 day'    : $period . ' days';
     }
     else if ($secsago < 2419200) {
         $period    =   round($secsago/604800);
         $period    =   $period == 1 ? '1 week'   : $period . ' weeks';
     }
     else if ($secsago < 29030400) {
         $period    =   round($secsago/2419200);
         $period    =   $period == 1 ? '1 month'   : $period . ' months';
     }
     else {
         $period    =   round($secsago/29030400);
         $period    =   $period == 1 ? '1 year'    : $period . ' years';
     }
     return $period;

 }

Then in the view files I showed the data as:

<div class="period">
        <?=getTimeDuration($created);?> ago
</div>

If you need similar task instead of writing a new function again, you can use the above function.

mahmud ahsan

Love computer programming and write occasional blog post. In my early career I worked mostly on web based application and since 2011, I exclusively working on iPhone and android applications. I work at home, father of a nice kid and husband of a beautiful wife. [My Business | Twitter | Linkedin]

You may also like

2 Comments

  • Justin Swanhart
    July 14, 2010 at 2:31 am

    You don’t account for leap seconds, and this requires that both timestamps have the same timezone.

    You might want to determine the best units, then make another call to the database using TIMESTAMPDIFF() to do the actual date arithmetic. You could use an IF or CASE structure to do it in one SQL call.

  • Ankur Verma
    August 20, 2012 at 2:46 pm

    this is great article…thanks a lot

Comments are closed here.

About Me

Hi I am Mahmud Ahsan. I Love computer programming and write occasional blog post. In my early career I worked mostly on web based application and since 2011, I exclusively working on iPhone and android applications. I work at home, father of a nice kid and husband of a beautiful wife. [My Business | Twitter | Linkedin]

Twitter Feed

Fanpage