douyan1321 2017-09-16 07:45
浏览 217
已采纳

可捕获的致命错误:类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();
}
  • 写回答

1条回答 默认 最新

  • doujiong9915 2017-09-16 07:51
    关注

    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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加
  • ¥15 用ns3仿真出5G核心网网元
  • ¥15 matlab答疑 关于海上风电的爬坡事件检测
  • ¥88 python部署量化回测异常问题
  • ¥30 酬劳2w元求合作写文章
  • ¥15 在现有系统基础上增加功能