ihazmore
ihazmore

Reputation: 1

php 5.3 date diff equivalent for PHP <= 5.2 on own function

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.

Upvotes: 0

Views: 7446

Answers (3)

nembleton
nembleton

Reputation: 2502

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.

Upvotes: 1

wormhit
wormhit

Reputation: 3827

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

?>

Upvotes: 0

Bert
Bert

Reputation: 1029

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.

Upvotes: 0

Related Questions