0

I'm trying to work with dates for the first time, I did it something about that with Flash but it's different.

I have two different dates and I'd like to see the difference in hours and days with them, I've found too many examples but not what I'm loking for:

<?php
    $now_date = strtotime (date ('Y-m-d H:i:s')); // the current date 
    $key_date = strtotime (date ("2009-11-21 14:08:42"));
    print date ($now_date - $key_date);
    // it returns an integer like 5813, 5814, 5815, etc... (I presume they are seconds) 
?>

How can I convert it to hours or to days?

6 Answers 6

5

The DateTime diff function returns a DateInterval object. This object consists of variabeles related to the difference. You can query the days, hours, minutes, seconds just like in the example above.

Example:

<?php 
 $dateObject = new DateTime(); // No arguments means 'now'
 $otherDateObject = new DateTime('2008-08-14 03:14:15');
 $diffObject = $dateObject->diff($otherDateObject)); 
 echo "Days of difference: ". $diffObject->days; 
?>

See the manual about DateTime.

Sadly, it's a PHP 5.3> only feature.

2
  • great suggestion, unfortunately I have a 5.22 version, but I'll use it next time I can.
    – vitto
    Nov 22, 2009 at 16:15
  • 1
    Good answer, one wee thing, in your example there, you have an extra brace on the 4th line. So... $diffObject = $dateObject->diff($otherDateObject)); should be... $diffObject = $dateObject->diff($otherDateObject); May 8, 2013 at 19:34
1

Well, you can always use date_diff, but that is only for PHP 5.3.0+

The alternative would be math.

How can I convert it [seconds] to hours or to days?

There are 60 seconds per minute, which means there are 3600 seconds per hour.

$hours = $seconds/3600;

And, of course, if you need days ...

$days = $hours/24;
0
1

If you dont have PHP5.3 you could use this method from userland (taken from WebDeveloper.com)

function date_time_diff($start, $end, $date_only = true)  // $start and $end as timestamps
{
    if ($start < $end) {
        list($end, $start) = array($start, $end);
    }
    $result = array('years' => 0, 'months' => 0, 'days' => 0);
    if (!$date_only) {
        $result = array_merge($result, array('hours' => 0, 'minutes' => 0, 'seconds' => 0));
    }
    foreach ($result as $period => $value) {
        while (($start = strtotime('-1 ' . $period, $start)) >= $end) {
            $result[$period]++;
        }
        $start = strtotime('+1 ' . $period, $start);
    }
    return $result;
}

$date_1 = strtotime('2005-07-31');
$date_2 = time();
$diff = date_time_diff($date_1, $date_2);
foreach ($diff as $key => $val) {
    echo $val . ' ' . $key . ' ';
}

// Displays:
// 3 years 4 months 11 days 
0

TheGrandWazoo mentioned a method for php 5.3>. For lower versions you can devide the number of seconds between the two dates with the number of seconds in a day to find the number of days.

For days, you do:

$days = floor(($now_date - $key_date) / (60 * 60 * 24))

If you want to know how many hours are still left, you can use the modulo operator (%)

$hours = floor((($now_date - $key_date) % * (60 * 60 * 24)) / 60 * 60)
0
<?php
    $now_date = strtotime (date ('Y-m-d H:i:s')); // the current date 
    $key_date = strtotime (date ("2009-11-21 14:08:42"));
    $diff = $now_date - $key_date;
    $days    = floor($diff/(60*60*24));
    $hours   = floor(($diff-($days*60*60*24))/(60*60));
    print $days." ".$hours." difference";
?>
0

I prefer to use epoch/unix time deltas. Time represented in seconds and as such you can very quickly divide by 3600 for hours and divide by 24*3600=86400 for days.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.