旧版本中PHP缺少功能

时间:2010-11-20 15:46:57

标签: php

我的这个PHP函数将日期时间字符串转换为更易读的方式来表示传递的日期和时间。这在PHP版本5.3.0中工作得很好,但在服务器端它是PHP版本5.2.17,缺少此功能。有没有办法可以有效地解决这个问题?这不仅是一个需要这种“差异”功能的功能,还有更多功能。

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 个答案:

答案 0 :(得分:3)

答案可以在您在php.net上找到代码的下方的注释中找到: http://php.net/manual/en/datetime.diff.php

<?php
function date_diff($date1, $date2) {
    $current = $date1;
    $datetime2 = date_create($date2);
    $count = 0;
    while(date_create($current) < $datetime2){
        $current = gmdate("Y-m-d", strtotime("+1 day", strtotime($current)));
        $count++;
    }
    return $count;
}

echo (date_diff('2010-3-9', '2011-4-10')." days <br \>");
?>

答案 1 :(得分:1)

我来到这个重写的功能,效果很好。

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"));
    }
}