可捕获的致命错误:DateTime 类的对象无法转换为字符串

2024-01-09

我在最后一行代码中收到上述错误,我已经尝试过答案,但这似乎不起作用

if (isset($_GET['logout'])) {
    $name = $_SESSION["username"];
    date_default_timezone_set('Asia/Kolkata');
    $today = date('Y-m-d');
    $time = new DateTime(date('H:i:s'));

    $statement = $db->prepare("SELECT  `logintime` FROM `attendance` WHERE empid=? AND date_t=?");
    $statement->bind_param("ss", $name, $today);
    $statement->execute();
    $statement->bind_result($logintime);
    while ($statement->fetch()) {

    }
    $logintime = new DateTime($logintime);

    $interval = $logintime->diff($time);

    $hours = $interval->format('%h');
    $minutes = $interval->format('%i');
    $workinghours = $hours + $minutes / 60;


    $stmt = $db->prepare("UPDATE `attendance` SET `logouttime`=? ,`workinghours`=?  WHERE empid=? AND date_t=?");
    $stmt->bind_param("ssss", $time, $workinghours, $name, $today);
    $run = $stmt->execute();
}

你必须使用format http://php.net/manual/en/datetime.format.php#107757方法将 DateTime 对象转换为 MySQL 可以使用的对象。

date_format($time,'Y-m-d H:i:s')

or

$time->format('Y-m-d H:i:s')

So...

$timeAsString = $time->format('Y-m-d H:i:s');
$stmt->bind_param("ssss",$timeAsString,$workinghours,$name,$today);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

可捕获的致命错误:DateTime 类的对象无法转换为字符串 的相关文章

随机推荐