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条)

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题