我想找出这两个日期与日,小时和分钟之间的差异

时间:2014-09-26 11:07:01

标签: php

我想找出这两个日期与天,小时和分钟之间的差异。

$date1 = "27-09-2014 05:00 AM";
$date2 = "29-09-2014 03:00 PM";

2 个答案:

答案 0 :(得分:1)

从PHP版本> 5添加新的日期/时间函数以获得差异:

$datetime1 = new DateTime("2010-06-20");
$datetime2 = new DateTime("2011-06-22");
$difference = $datetime1->diff($datetime2);

echo 'Difference: '.$difference->y.' years, ' 
                   .$difference->m.' months, ' 
                   .$difference->d.' days';

print_r($difference);

结果如下:

Difference: 1 years, 0 months, 2 days

DateInterval Object
(
    [y] => 1
    [m] => 0
    [d] => 2
    [h] => 0
    [i] => 0
    [s] => 0
    [invert] => 0
    [days] => 367
)

答案 1 :(得分:0)

我不确定我的问题是否正确,但不应该做以下工作?

$datetime1 = new DateTime('27-09-2014');
$datetime2 = new DateTime('29-09-2014');

$datetime1->setTime(05, 00);
$datetime2->setTime(15, 00);


$interval = date_diff($datetime1, $datetime2);
echo $interval->format('%a Day and %h hours'

DateTime::diff