Convert dates to hours

2019-04-17 07:13发布

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?

标签: php date hour days
6条回答
时光不老,我们不散
2楼-- · 2019-04-17 07:20

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.

查看更多
狗以群分
3楼-- · 2019-04-17 07:21

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)
查看更多
Bombasti
4楼-- · 2019-04-17 07:24

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 
查看更多
够拽才男人
5楼-- · 2019-04-17 07:27

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;
查看更多
时光不老,我们不散
6楼-- · 2019-04-17 07:29
<?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";
?>
查看更多
相关推荐>>
7楼-- · 2019-04-17 07:39

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.

查看更多
登录 后发表回答