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

I am getting the above error in the last line of code ,I have tried answers but that doesnt seems to work

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();
}

you have to use the format method to convert your DateTime object into something MySQL can use.

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);