Umair A.
Umair A.

Reputation: 6873

PHP Missing Function In Older Version

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

Upvotes: 1

Views: 592

Answers (2)

Umair A.
Umair A.

Reputation: 6873

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

Upvotes: 1

Chuck Burgess
Chuck Burgess

Reputation: 11574

The answer can be found in the comment right below the one you found your code in on 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 \>");
?>

Upvotes: 3

Related Questions