将日期时间转换为字符串时出错,无法将类 DateTime 的对象转换为字符串



由于将日期时间转换为字符串,第 18 行出现此代码错误,我在没有(字符串(的情况下尝试过它,并且遇到了同样的问题,尽管它确实在另一个页面上有效

<?php
$con = mysqli_connect("localhost","workAcc","storm111274","work"); 
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
echo "Tell Bertie or who ever does IT";
}
$sqlSelect = "SELECT * FROM downstairs WHERE ID = ".htmlspecialchars($_GET["ID"]) .";";
$result = mysqli_query($con, $sqlSelect);
if (mysqli_num_rows($result)  > 1){
echo "error too many rows this really really shouldn't happen";
}elseif(mysqli_num_rows($result)  === 0){
 echo "No record found";  
}else{
while ($row = mysqli_fetch_assoc($result)) {
$sDate = date_create($row["StartTime"]);
$eDate = date_create($row["EndTime"]);
echo $row["ID"] . "_" . $row["Course"] . "_" . (string)date_format($sDate, "H:i") . "_" . (string)date_format($eDate, "H:i") . "_" . $eDate . "_" . $row["Room"] . "_" . $row["Instructor"];
}}
?>

这个连接的字符串中有一个$eDate

echo $row["ID"] . "_" . $row["Course"] . "_" . (string)date_format($sDate, "H:i") . "_" 
    . (string)date_format($eDate, "H:i") . "_" . $eDate  // <<<< Here
    . "_" . $row["Room"] . "_" . $row["Instructor"];

$eDate是一个DateTime对象。当你用.它连接到字符串时,PHP 会尝试将其转换为字符串,这就是你得到你看到的错误的时候。若要修复此错误,请删除$eDate或将其转换为带有 date_format 的字符串。

我不确定您希望它的外观如何,所以我无法建议修复它的方法,但是不需要对其他date_format结果进行(string)转换。

试试这个:

date_format($sDate, "H:i"(->format('H:i'(

相关内容

  • 没有找到相关文章