0

I am using a function (found it on the web) that calculates the time that has passed until now. I am passing two parameters: post date and current date. it will return years, months, days, hours, minutes OR seconds. it uses PHP 5.3's date diff function which won't do in version 5.2 :(

function pluralize( $zaehler, $inhalt ) { 
return trim($zaehler . ( ( $zaehler == 1 ) ? ( " $inhalt" ) : ( " ${inhalt}s" ) )." ago");}function ago($datetime, $datetime_post){     

$interval = date_create($datetime_post)->diff( date_create($datetime) );

if ( $interval->y >= 1 ) return pluralize( $interval->y, 'year' );
if ( $interval->m >= 1 ) return pluralize( $interval->m, 'month' );
if ( $interval->d >= 1 ) return pluralize( $interval->d, 'day' );
if ( $interval->h >= 1 ) return pluralize( $interval->h, 'hour' );
if ( $interval->i >= 1 ) return pluralize( $interval->i, 'minute' );
if ( $interval->s >= 1 ) return pluralize( $interval->s, 'second' );}

example:

$post_date_time = "01/01/2012 11:30:22";
$current_date_time = "02/02/2012 07:35:41";

echo ago($current_date_time, $post_date_time);

will output:

1 month

Now I need a equivalent function "ago" that would just do the same, depending on the $interval object.

thank you so much

Additional Info: None of the provided solutions actually did what I was looking for. I have to improve my explaining, sorry. At the end, I need only the $interval object to be having this:

object(DateInterval)#3 (8) { ["y"]=> int(0) ["m"]=> int(1) ["d"]=> int(0) ["h"]=> int(20) ["i"]=> int(5) ["s"]=> int(19) ["invert"]=> int(0) ["days"]=> int(6015) }

The no need to change so many things.

ihazmore
  • 1
  • 1
  • 3
  • Can't you use http://stackoverflow.com/questions/676824/how-to-calculate-the-difference-between-two-dates-using-php ? – ale Feb 21 '12 at 07:34
  • Also, you can get the current date time using PHP's `date( ... )` function in case you didn't know: http://php.net/manual/en/function.date.php – ale Feb 21 '12 at 07:36
  • It is very easy to get the difference of two dates, in seconds. Converting seconds to hours, minutes and days is trivial. Converting to months and years... that easy if you assume that every month is 30 days long and every year has 12 30-day months. Is this solution acceptable? – Salman A Feb 21 '12 at 07:41
  • You may want to take a look at Zend Framework's Zend_Date-component: http://framework.zend.com/manual/en/zend.date.basic.html#zend.date.simple.functions.compare To be clear, you do not need the complete Zend Framework, only take Zend/Date.php, Zend/Date/ and Zend/Locale.php (dependency). Then you should be able to use `new Zend_Date()` in your code... – dbrumann Feb 21 '12 at 07:41
  • @vivid-colours: thanks, unfortunately not. the output I am expecting should be one type only, like "2 months ago", or "34 minutes ago". same like twitter/fb. – ihazmore Feb 21 '12 at 11:33
  • @Salman/mahok. Thanks. I don't want to calculate in seconds and convert afterwards. I simply want to adjust my function (not really mine) above so you'd simply feed two dates and it returns "whatsoever ago"... – ihazmore Feb 21 '12 at 11:35
  • is there a way to have an object like the following returned? object(DateInterval)#3 (8) { ["y"]=> int(0) ["m"]=> int(1) ["d"]=> int(0) ["h"]=> int(20) ["i"]=> int(5) ["s"]=> int(19) ["invert"]=> int(0) ["days"]=> int(6015) } – ihazmore Feb 22 '12 at 02:46
  • I just posted an extensive answer to a duplicate question [here][1] [1]: http://stackoverflow.com/questions/4033224/what-can-use-for-datetimediff-for-php-5-2 – nembleton Jul 09 '12 at 06:19

3 Answers3

1

I just needed that ( unfortunately ) for a WordPress plugin. This I use the function in 2 times. I posted this answer here:

  1. In my class calling ->diff() ( my class extends DateTime, so $this is the reference DateTime )

    function diff ($secondDate){
        $firstDateTimeStamp = $this->format("U");
        $secondDateTimeStamp = $secondDate->format("U");
        $rv = ($secondDateTimeStamp - $firstDateTimeStamp);
        $di = new DateInterval($rv);
        return $di;
    }
    
  2. Then I recreated a fake DateInterval class ( because DateInterval is only valid in PHP >= 5.3 ) as follows:

    Class DateInterval {
        /* Properties */
        public $y = 0;
        public $m = 0;
        public $d = 0;
        public $h = 0;
        public $i = 0;
        public $s = 0;
    
        /* Methods */
        public function __construct ( $time_to_convert /** in seconds */) {
            $FULL_YEAR = 60*60*24*365.25;
            $FULL_MONTH = 60*60*24*(365.25/12);
            $FULL_DAY = 60*60*24;
            $FULL_HOUR = 60*60;
            $FULL_MINUTE = 60;
            $FULL_SECOND = 1;
    
    //        $time_to_convert = 176559;
            $seconds = 0;
            $minutes = 0;
            $hours = 0;
            $days = 0;
            $months = 0;
            $years = 0;
    
            while($time_to_convert >= $FULL_YEAR) {
                $years ++;
                $time_to_convert = $time_to_convert - $FULL_YEAR;
            }
    
            while($time_to_convert >= $FULL_MONTH) {
                $months ++;
                $time_to_convert = $time_to_convert - $FULL_MONTH;
            }
    
            while($time_to_convert >= $FULL_DAY) {
                $days ++;
                $time_to_convert = $time_to_convert - $FULL_DAY;
            }
    
            while($time_to_convert >= $FULL_HOUR) {
                $hours++;
                $time_to_convert = $time_to_convert - $FULL_HOUR;
            }
    
            while($time_to_convert >= $FULL_MINUTE) {
                $minutes++;
                $time_to_convert = $time_to_convert - $FULL_MINUTE;
            }
    
            $seconds = $time_to_convert; // remaining seconds
            $this->y = $years;
            $this->m = $months;
            $this->d = $days;
            $this->h = $hours;
            $this->i = $minutes;
            $this->s = $seconds;
        }
    }
    

Hope that helps somebody.

Community
  • 1
  • 1
nembleton
  • 2,392
  • 1
  • 18
  • 20
0

try what im using.

function dateDiff($dformat, $endDate, $beginDate)
{
$date_parts1=explode($dformat, $beginDate);
$date_parts2=explode($dformat, $endDate);
$start_date=gregoriantojd($date_parts1[0], $date_parts1[1], $date_parts1[2]);
$end_date=gregoriantojd($date_parts2[0], $date_parts2[1], $date_parts2[2]);
return $end_date - $start_date;
}

the $dformat is the delimiter your using in the date.

Bert
  • 1,019
  • 3
  • 14
  • 25
  • Why don't you use strtotime() - http://php.net/manual/en/function.strtotime.php ? – Timur Feb 21 '12 at 07:41
  • for me gregoriantojd is better bcoz i don't have to worry about the date format since it takes date parts as parameter and the function returns in days. but can also use strtotime(); – Bert Feb 21 '12 at 07:57
0

There is a simple way. You can change some things in there so it fits your needs.

<?php //preparing values
date_default_timezone_set('Europe/Berlin');

$startDate = '2011-01-21 09:00:00';
$endDate = date('Y-m-d H:i:s');

// time span seconds
$sec = explode(':', (gmdate('Y:m:d:H:i:s', strtotime($endDate) - strtotime($startDate))));

// getting all the data into array
$data = array();
list($data['years'], $data['months'], $data['days'], $data['hours'], $data['minutes'], $data['seconds']) = $sec;
$data['years'] -= 1970;

var_dump($data);

?>
wormhit
  • 3,687
  • 37
  • 46