开发者

Pretty printing library for time differences in PHP [closed]

开发者 https://www.devze.com 2023-01-29 22:33 出处:网络
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.

Questions asking us to recommend or find a tool, library or favorite off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it.

Closed 9 years ago.

开发者_开发知识库 Improve this question

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?


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;
 }
?>


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?


PHP has a class built-in for this from PHP 5.3 onwards:

DateInterval

Use the format() method to get the output you wish for, though you need to decide on the necessary units I think.


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);
0

精彩评论

暂无评论...
验证码 换一张
取 消