<?php
$ts='2011-04-13 23:00:00';
$ts1='2011-04-14 15:45:00';
echo $addtime = date("h:i:s", mktime(date("h", $ts1)- date("h", $ts),date("i", $ts1)- date("i", $ts),date("s", $ts1)- date("s", $ts),0,0,0));
?>
它给出了结果但在许多情况下它是不正确的。我该如何解决?
答案 0 :(得分:2)
对于给定的示例,您的预期结果将是16:45:00,对吗?因此,您希望两个给定日期之间的差异以小时为单位:分钟:秒。
<?php
//initial strings
$ts='2011-04-13 23:00:00';
$ts1='2011-04-14 15:45:00';
//converting to time
$start = strtotime($ts);
$end = strtotime($ts1);
//calculating the difference
$difference = $end - $start;
//calculating hours, minutes and seconds (as floating point values)
$hours = $difference / 3600; //one hour has 3600 seconds
$minutes = ($hours - floor($hours)) * 60;
$seconds = ($minutes - floor($minutes)) * 60;
//formatting hours, minutes and seconds
$final_hours = floor($hours);
$final_minutes = floor($minutes);
$final_seconds = floor($seconds);
//output
echo $final_hours . ":" . $final_minutes . ":" . $final_seconds;
?>
这给了我正确的结果。希望我的问题正确!