时间差异

时间:2014-03-07 11:33:11

标签: php

我正在制作测验模块。我必须找到用户花多少时间完成测验。为此,我正在做一些我在下面给出的代码:

$_SESSION['start'] = mktime(date('h'), date('i'), date('s'), date("m")  , date("d"), date("Y"));
$_SESSION['endtime'] = mktime(date('h'), date('i'), date('s'), date("m")  , date("d"), date("Y"));
if(isset($_SESSION['endtime']))
{
    $date1 = $_SESSION['start']; 
    $date2 = $_SESSION['endtime'];
    $dateDiff = $date2 - $date1;
    $fullDays = floor($dateDiff/(60*60*24));
    $fullHours = sprintf("%02d",floor(($dateDiff-($fullDays*60*60*24))/(60*60)));
    $fullMinutes = sprintf("%02d",floor(($dateDiff-($fullDays*60*60*24)-($fullHours*60*60))/60));
    $fullSec = sprintf("%02d",floor(($dateDiff-($fullDays*60*60*24)-($fullHours*60*60)-($fullMinutes*60))));
    $time="$fullHours:$fullMinutes:$fullSec";   
}
else
{
    $time="over";
}

但是没有得到适当的时间。我能做什么?

2 个答案:

答案 0 :(得分:0)

if(isset($_SESSION['starttime']))
{
    $date1 = $_SESSION['starttime']; 
    $date2 = time();
    $dateDiff = $date2 - $date1;
    $time=date('H:i:s', $dateDiff); // thank you Wilmer :)   
}
else
{
    $_SESSION['starttime'] = time();
    $time="00:00:00";
}

答案 1 :(得分:0)

以下内容应该可行,但尚未测试过:

$_SESSION['start'] = strtotime(date('H:i:s'));
$_SESSION['endtime'] = strtotime(date('H:i:s'));
if(isset($_SESSION['endtime']))
{
    $date1 = $_SESSION['start']; 
    $date2 = $_SESSION['endtime'];
    $dateDiff = $date2 - $date1;
    $time=date('H:i:s', $dateDiff);   
}
else
{
    $time="over";
}