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.

About mahmud ahsan

Programming is my passion from high school. I always looking for innovating new ideas and making them real application. I love mobile application and game development as well as web development. Having 4+ years web development experience and 6+ years mobile application development experience on different platform and business on it. My Mobile Apps Portfolio: iThinkdiff.net Mobile Apps

, , , , ,

2 Responses to Retrieve time period like facebook fashion

  1. 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.

  2. Ankur Verma August 20, 2012 at 2:46 pm #

    this is great article…thanks a lot