1
<?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));
?>

它给出了一个结果,但在许多情况下它是不正确的。我如何解决它?

4

1 回答 1

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;
?>

这给了我正确的结果。希望我能解决你的问题!

于 2012-11-20T09:47:00.030 回答