Monday, March 3, 2014

PHP Time Ago

A simple way to display ago with UNIX time.







Displaying the time ago.
To display the time ago. Just add this where you want to display the time ago.
<?PHP
/*Created by Barrett at RRPowered.com*/
include_once('ago.php');//Include the ago.php file 
$cur_time1 ="1291684422";//Your Unix time from database
echo time_ago($cur_time1) ;
?>

Here is the PHP file!
This file is called ago.php

<?PHP
/*Created by Barrett at RRPowered.com*/
//Call the function
function time_ago($cur_time){
$time_ = time() - $cur_time;
 
$seconds =$time_;
$minutes = round($time_ / 60);
$hours = round($time_ / 3600);
$days = round($time_ / 86400);
$weeks = round($time_ / 604800);
$months = round($time_ / 2419200);
$years = round($time_ / 29030400);
 
//Seconds
if($seconds <= 60){ 
   $time="$seconds seconds ago";   
 
//Minutes    
}else if($minutes <= 60){ 
   if($minutes == 1){
   $time="one minute ago";
   }else{
   $time="$minutes minutes ago";
   } 
//Hours
}else if($hours <= 24){ 
  if($hours == 1){
  $time="one hour ago";
  }else{
  $time="$hours hours ago";
  } 
//Days 
}else if($days <= 7){ 
   if($days == 1){
   $time="one day ago";
   }else{
   $time="$days days ago";
   } 
//Weeks
}else if($weeks <= 4){ 
  if($weeks == 1){
  $time="one week ago";
  }else{
  $time="$weeks weeks ago";
  } 
//Months  
}else if($months <= 12){ 
  if($months == 1){
  $time="one month ago";
  }else{
  $time="$months months ago";
  } 
//Years 
}else{ 
  if($year == 1){
  $time="one year ago";
  }else{
  $time="$year years ago";
  }  
}
return $time;
}
?>

Source Form: http://www.rrpowered.com

No comments:

Post a Comment