PHP为什么DateTime diff函数总是返回零?
我有下面的代码试图计算工作时间。我有点问题,小时和分钟总是归零。我最好的猜测是它与新的日期时间有关,但我不知道如何补救PHP为什么DateTime diff函数总是返回零?,php,datetime,date-math,Php,Datetime,Date Math,我有下面的代码试图计算工作时间。我有点问题,小时和分钟总是归零。我最好的猜测是它与新的日期时间有关,但我不知道如何补救 <?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 = html
<?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 />";
?>
这两次之间的时间差为零,所以这是有意义的。您需要使用两个相距较远的时间进行测试,以在此处看到大于零的值 您的会议记录不正确,因为
m
是月份标识符<代码>i用于分钟
$minutes = $diff->i;
$minutes = $diff->i;