Is there any good pretty printing library for UNIX timestamp differences . Something on the likes of the things seen in social networking sites like "x minutes ago" , "x hours ago" , "x days ago" etc . I know that it won't be hard to write on my own but why reinvent the wheel?
            Asked
            
        
        
            Active
            
        
            Viewed 2,872 times
        
    1
            
            
        - 
                    I answered this for someone else recently: http://stackoverflow.com/questions/4394161/php-time-calculation/4394181#4394181 – El Yobo Dec 12 '10 at 08:29
4 Answers
2
            
            
        try
<?php
function rel_time($from, $to = null)
 {
  $to = (($to === null) ? (time()) : ($to));
  $to = ((is_int($to)) ? ($to) : (strtotime($to)));
  $from = ((is_int($from)) ? ($from) : (strtotime($from)));
  $units = array
  (
   "year"   => 29030400, // seconds in a year   (12 months)
   "month"  => 2419200,  // seconds in a month  (4 weeks)
   "week"   => 604800,   // seconds in a week   (7 days)
   "day"    => 86400,    // seconds in a day    (24 hours)
   "hour"   => 3600,     // seconds in an hour  (60 minutes)
   "minute" => 60,       // seconds in a minute (60 seconds)
   "second" => 1         // 1 second
  );
  $diff = abs($from - $to);
  $suffix = (($from > $to) ? ("from now") : ("ago"));
  foreach($units as $unit => $mult)
   if($diff >= $mult)
   {
    $and = (($mult != 1) ? ("") : ("and "));
    $output .= ", ".$and.intval($diff / $mult)." ".$unit.((intval($diff / $mult) == 1) ? ("") : ("s"));
    $diff -= intval($diff / $mult) * $mult;
   }
  $output .= " ".$suffix;
  $output = substr($output, strlen(", "));
  return $output;
 }
?>
 
    
    
        Anush Prem
        
- 1,511
- 8
- 16
1
            I don't know if there's a library for PHP for this, but Jeff (who created this website) asked this question a long time ago. See this question for details. I'm sure you could get a lot of inspiration from that. Jeff even answers that question with the code they use on StackOverflow itself.
I don't think it would be that hard to write this yourself, so why not spend 5 minutes writing it instead of half an hour looking for a library?
 
    
    
        Community
        
- 1
- 1
 
    
    
        AgentConundrum
        
- 20,288
- 6
- 64
- 99
0
            
            
        defined('SECOND') ? NULL : define('SECOND', 1);
defined('MINUTE') ? NULL : define('MINUTE', 60 * SECOND);
defined('HOUR') ? NULL : define('HOUR', 60 * MINUTE);
defined('DAY') ? NULL : define('DAY', 24 * HOUR);
defined('MONTH') ? NULL : define('MONTH', 30 * DAY);
defined('YEAR') ? NULL : define('YEAR', 12 * MONTH);
class Time{
    public $td;
    public function set_td($timestamp){
        $this->td = time() - $timestamp;
    }
    public function string_time($timestamp){
        $this->set_td($timestamp);
        if ($this->td < 0){
            return "not yet";
        }
        if ($this->td < 1 * MINUTE){
             return $this->td <= 1 ? "just now" : $this->td." seconds ago";
        }
        if ($this->td < 2 * MINUTE){
          return "a minute ago";
        }
        if ($this->td < 45 * MINUTE){
            return floor($this->td / MINUTE)." minutes ago";
        }
        if ($this->td < 90 * MINUTE){
            return "an hour ago";
        }
        if ($this->td < 24 * HOUR){
            return floor($this->td / HOUR)." hours ago";
        }
        if ($this->td < 48 * HOUR){
            return "yesterday";
        }
        if ($this->td < 30 * DAY){
             return floor($this->td / DAY)." days ago";
        }
        if ($this->td < 12 * MONTH){
            $months = floor($this->td / MONTH);
            return $months <= 1 ? "one month ago" : $months." months ago";
        }else{
            $years = floor($this->td / YEAR);
            return $years <= 1 ? "one year ago" : $years." years ago";
        }
     }
 }
$time = new Time();
Simply put your timestamp into the new object and recievi the time diffenrece in seconds.e.g.
$string = $time->string_time($timestamp);
 
    
    
        yehuda
        
- 1,254
- 2
- 11
- 21
 
    