-1

我有以下代码试图计算工作时间。在那几小时和几分钟内我遇到了一些问题,总是返回零。我最好的猜测是它与新的 DateTime 有关,但我不知道如何解决这个问题。

<?php
include_once 'db_connect.php';
include_once 'psl-config.php';
include_once 'functions.php';

$error_msg = "";

sec_session_start();

//Get Username from session id
$username =  htmlentities($_SESSION['username']);

//get time date stamp
date_default_timezone_set('America/Chicago'); //set default timezone, just to be sure


//GET Last Clockin from timesheet table
$result = $mysqli->query("SELECT lastclockin FROM timesheet WHERE username = '$username'");
$number_of_rows =  $result->num_rows;

while ($row = mysqli_fetch_assoc($result)) {
        $lastclockin = $row["lastclockin"];
    }
    echo $lastclockin;
    echo "<br />";

    $now = new DateTime();

    $diff = $now->diff(new DateTime($lastclockin));

$hours = $diff->h;
$minutes = $diff->m;
$hours = $hours + ($diff->days*24);

echo $now->format('Y-m-d H:i:s');
echo "<br />";

echo $hours;
echo "<br />";
echo $minutes;
echo "<br />";
?>

结果如下:

2014-09-11 13:02:15
2014-09-11 13:47:34
0
0
4

1 回答 1

3

两次之间的时间差为零,所以这是有道理的。您需要使用相距较远的两次进行测试,才能在此处看到大于零的值。

您的分钟不正确,因为m是月份标识符。i是几分钟。

$minutes = $diff->i;
于 2014-09-11T18:57:07.347 回答