1

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 Answers 2

3

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 \>");
?>
Sign up to request clarification or add additional context in comments.

6 Comments

and you can add it to your application with if(!function_exists('date_diff')) { // declare function here } so you wont have to bother which version is on the server.
but I specifically need the function "diff"
@Umair See the manual. date_diff is the procedural alias of Date::diff
@Gordon: the function will always output difference of days
@Gordon: Thanks for your efforts. I thought I should write that function myself and I did it. I am answering my own question now.
|
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.