在PHP中使用'Y-m-d H:i:s'格式获得差异时间?

时间:2013-11-21 04:12:18

标签: php

示例我有

$time1 = "2013-11-21 12:59:00";
$time2 = "2013-11-21 13:01:00";

如何获得两次以上格式的差异“Y-m-d H:i:s”??

3 个答案:

答案 0 :(得分:3)

试试这个:

  $date1 = new DateTime("2013-11-21 12:59:00");
    $date2 = new DateTime("2013-11-21 13:01:00");
    $interval = $date1->diff($date2); 
echo "DIFF: ".$interval->format("%Y-%m-%d %H:%i:%s");

答案 1 :(得分:1)

$time1 = new DateTime('2013-11-21 12:59:00');
$time2 = new DateTime('2013-11-21 13:01:00');
$interval = $time1->diff($time2);

echo $interval->y . " Year " .$interval->m . " Month " .$interval->d ." Days ". $interval->h . " Hours, " . $interval->i." Mintues, ".$interval->s." seconds <br/>"; 

答案 2 :(得分:0)

$time1 = strtotime("2013-11-21 12:59:00");
$time2 = strtotime("2013-11-21 12:59:00");
$diff = date("Y-m-d H:i:s", $time2-$time1);
echo $diff;