dsfg3241 2010-11-20 15:46
浏览 58
已采纳

旧版本中PHP缺少功能

My this PHP function converts a datetime string into more readable way to represent passed date and time. This is working perfect in PHP version 5.3.0 but on the server side it is PHP version 5.2.17 which lacks this function. Is there a way I can fix this efficiently? This is not only a function which needs this "diff" function but there are many more.

public function ago($dt1)
 {
  $interval = date_create('now')->diff(date_create($dt1));
  $suffix = ($interval->invert ? ' ago' : '-');
  if ($v = $interval->y >= 1) return $this->pluralize($interval->y, 'year') . $suffix;
  if ($v = $interval->m >= 1) return $this->pluralize($interval->m, 'month') . $suffix;
  if ($v = $interval->d >= 1) return $this->pluralize($interval->d, 'day') . $suffix;
  if ($v = $interval->h >= 1) return $this->pluralize($interval->h, 'hour') . $suffix;
  if ($v = $interval->i >= 1) return $this->pluralize($interval->i, 'minute') . $suffix;
   return $this->pluralize($interval->s, 'second') . $suffix;
 } 
  • 写回答

2条回答 默认 最新

  • dongshang1768 2010-11-22 07:38
    关注

    I came to this re-written function which works pretty well.

    class DateTimeFormatter
    {
        private $now;
        public $invert;
        public $y, $m, $d, $h, $i, $s;
    
        function DateTimeFormatter()
        {
            $this->now = date_parse(date("Y-m-d H:i:s"));   
        }
    
        function now()
        {
            $this->now = date_parse(date("Y-m-d H:i:s"));
            return $this->now;
        }
    
        function diff($dt)
        {
            if (!is_array($dt))
                $dt = date_parse($dt);
    
            if ($this->now() > $dt)
            {
                $this->invert = 1;
                $this->y = $this->now['year'] - $dt['year'];
                $this->m = $this->now['month'] - $dt['month'];
                $this->d = $this->now['day'] - $dt['day'];
                $this->h = $this->now['hour'] - $dt['hour'];
                $this->i = $this->now['minute'] - $dt['minute'];
                $this->s = $this->now['second'] - $dt['second'];
            }
            else
            {
                $this->invert = 0;
                $this->y = $dt['year'] - $this->now['year'];
                $this->m = $dt['month'] - $this->now['month'];
                $this->d = $dt['day'] - $this->now['day'];
                $this->h = $dt['hour'] - $this->now['hour'];
                $this->i = $dt['minute'] - $this->now['minute'];
                $this->s = $dt['second'] - $this->now['second'];
            }
    
            return $this;                            
        }
    
        function ago($datetime)
        {
            $interval = $this->diff($datetime);
            $suffix = ($interval->invert ? ' ago' : '');
            if ($v = $interval->y >= 1) return $this->pluralize($interval->y, 'year') . $suffix;
            if ($v = $interval->m >= 1) return $this->pluralize($interval->m, 'month') . $suffix;
            if ($v = $interval->d >= 1) return $this->pluralize($interval->d, 'day') . $suffix;
            if ($v = $interval->h >= 1) return $this->pluralize($interval->h, 'hour') . $suffix;
            if ($v = $interval->i >= 1) return $this->pluralize($interval->i, 'minute') . $suffix;
            return $this->pluralize($interval->s, 'second') . $suffix;
        }
    
        function pluralize( $count, $text )
        {
            return $count . (($count == 1) ? (" $text") : (" ${text}s"));
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?