gpt4 book ai didi

php - 可捕获的 fatal error : Object of class DateTime could not be converted to string

转载 作者:太空宇宙 更新时间:2023-11-03 11:35:40 25 4
gpt4 key购买 nike

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

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将 DateTime 对象转换为 MySQL 可以使用的对象的方法。

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

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

所以...

$timeAsString = $time->format('Y-m-d H:i:s');
$stmt->bind_param("ssss",$timeAsString,$workinghours,$name,$today);

关于php - 可捕获的 fatal error : Object of class DateTime could not be converted to string,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46251508/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com