Pages

Sunday, August 18, 2013

PHP-Convert a timestamp into facebook post time representation i.e years ago,months ago,weeks ago,days ago,hours ago,minutes ago,seconds ago format

This code is helpful for converting a timestamp field to a time stated as

years ago if it is more than an year ago
months ago if it is more than a month ago
week ago
days ago
hours ago
seconds ago

For example a post made yesterday will be shown as " 1 day ago"

Here goes the code
 <?php  
 $today = time();    
                 $createdday= strtotime($post['created']); //mysql timestamp of when post was created  
                 $datediff = abs($today - $createdday);  
                 $difftext="";  
                 $years = floor($datediff / (365*60*60*24));  
                 $months = floor(($datediff - $years * 365*60*60*24) / (30*60*60*24));  
                 $days = floor(($datediff - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24));  
                 $hours= floor($datediff/3600);  
                 $minutes= floor($datediff/60);  
                 $seconds= floor($datediff);  
                 //year checker  
                 if($difftext=="")  
                 {  
                   if($years>1)  
                    $difftext=$years." years ago";  
                   elseif($years==1)  
                    $difftext=$years." year ago";  
                 }  
                 //month checker  
                 if($difftext=="")  
                 {  
                    if($months>1)  
                    $difftext=$months." months ago";  
                    elseif($months==1)  
                    $difftext=$months." month ago";  
                 }  
                 //month checker  
                 if($difftext=="")  
                 {  
                    if($days>1)  
                    $difftext=$days." days ago";  
                    elseif($days==1)  
                    $difftext=$days." day ago";  
                 }  
                 //hour checker  
                 if($difftext=="")  
                 {  
                    if($hours>1)  
                    $difftext=$hours." hours ago";  
                    elseif($hours==1)  
                    $difftext=$hours." hour ago";  
                 }  
                 //minutes checker  
                 if($difftext=="")  
                 {  
                    if($minutes>1)  
                    $difftext=$minutes." minutes ago";  
                    elseif($minutes==1)  
                    $difftext=$minutes." minute ago";  
                 }  
                 //seconds checker  
                 if($difftext=="")  
                 {  
                    if($seconds>1)  
                    $difftext=$seconds." seconds ago";  
                    elseif($seconds==1)  
                    $difftext=$seconds." second ago";  
                 }  
                 echo " | ".$difftext;  
 ?>  

4 comments:

  1. This worked brilliantly... many thanks for sharing.

    ReplyDelete
  2. I hope you will help me. I have a table that is a ToDo list and I want to show how long it has been on the list. I can display everything the way I want it but cannot get the X days ago to work. I looked at your code but still cannot figure out what I would need to change to work in mine. Any help would be great as I am new to learning PHP and mysql. THANKS

    ReplyDelete
  3. Hi Daniel can you provide me table structure of To Do list and code you are using to implement the same

    ReplyDelete