2

I am trying to calculate the time interval between two Dates. So here is my code

    $start_date = new DateTime("$date1");
$end_date = new DateTime("$date2");
$interval = $start_date->diff($end_date);

$days = $interval->d;
$months = $interval->m;
$years = $interval->y;
$hours = $interval->h;
$mins = $interval->i;
$secs = $interval->s;

when i try to echo the result it returns an empty result.

This same code worked on my development server and localhost, As soon as i changed to another server it stoped responding.

I checked php.ini for timezone setting, it was good.

I will be thank full for your help

Ramaraju.d
  • 1,301
  • 6
  • 26
  • 46

3 Answers3

10

Check your php.ini files inside /etc/php5/apache2/ and /etc/php5/cli/ folders. Both files must have declared the default time zone eg. date.timezone = "America/New_York"

Other way you can have it to work is declaring it inside your code before creating the DateTime object

date_default_timezone_set('America/New_York');
$start_date = new DateTime("$date1");
$end_date = new DateTime("$date2");
$interval = $start_date->diff($end_date);

$days = $interval->d;
$months = $interval->m;
$years = $interval->y;
$hours = $interval->h;
$mins = $interval->i;
$secs = $interval->s;
elvismdev
  • 1,027
  • 11
  • 21
1

NOTE: I suggest that you store everything in timestamp in your database rather than in date. And in your display logic, convert it to date, or find how old it is, etc. Read this on Datetime vs timestamp Should I use field 'datetime' or 'timestamp'?

Below,find two functions to find time difference between two timestamps as well as get the age of an old timestamp compared to now.

<?php  
echo whatAge(#SOME TIMESTAMP HERE#)
echo getDiff(#TIMESTAMP1, TIMESTAMP2#)
?>

This gives you day hour minute second. Modify the secondsToTime function below for something else.

Use the functions below...

function whatAge($old_time){
$current_time   =   getdate(time())[0];
$time_diff      =   $current_time-$old_time;
return secondsToTime($time_diff,'string');
}

function getDiff($time1, $time2){
$time_diff      =   $time1-$time2;
return secondsToTime($time_diff,'string');
}

function secondsToTime($inputSeconds, $return='array') {

/** https://stackoverflow.com/questions/8273804/convert-seconds-into-days-hours-minutes-and-seconds */

$secondsInAMinute = 60;
$secondsInAnHour  = 60 * $secondsInAMinute;
$secondsInADay    = 24 * $secondsInAnHour;

// extract days
$days = floor($inputSeconds / $secondsInADay);

// extract hours
$hourSeconds = $inputSeconds % $secondsInADay;
$hours = floor($hourSeconds / $secondsInAnHour);

// extract minutes
$minuteSeconds = $hourSeconds % $secondsInAnHour;
$minutes = floor($minuteSeconds / $secondsInAMinute);

// extract the remaining seconds
$remainingSeconds = $minuteSeconds % $secondsInAMinute;
$seconds = ceil($remainingSeconds);

// return the final array
$obj    = array(
        'd' => (int) $days,
        'h' => (int) $hours,
        'm' => (int) $minutes,
        's' => (int) $seconds,
);

$str    =   function($d,$h,$m,$s){
    if($d>0){
        return "$d d, $h h $m m";
    }else{
        return "$h h $m m";
    }
};

switch ($return){

        case    'array'  :  return $obj;    break;
        case    'string' :  return $str($obj['d'],$obj['h'],$obj['m'],$obj['s']);   break;

    }
}
Community
  • 1
  • 1
dbx834
  • 87
  • 6
0

Simple Way

Try this:

$date1 = new DateTime("2007-03-24");
$date2 = new DateTime("2009-06-26");
$interval = $date1->diff($date2);
echo "difference " . $interval->y . " years, " . $interval->m." months, ".$interval->d." days "; 

read more [php DateTime::diff manual][1]

sergio
  • 5,210
  • 7
  • 24
  • 46