class DateTime 的对象在我的查询中无法转换为字符串。我在这里做错了什么?
Object of class DateTime could not be converted to string in my query. What am I doing wrong here?
我在 php/mysql 中有以下查询:
$q2 = $conn3->prepare("SELECT (t.start_time + INTERVAL t.text_duration SECOND) as end_time FROM texts t WHERE t.start_time <= :starttime AND (t.start_time + INTERVAL t.text_duration SECOND) >= :starttime AND t.plot_id = :plot_id LIMIT 1");
$q2->bindValue(':starttime', $start_timestamp);
$q2->bindValue(':plot_id', $plot_id);
$q2->execute();
$check2 = $q2->fetch(PDO::FETCH_ASSOC);
$start_time
是一个日期时间对象,定义如下:
$date = new DateTime();
$start_timestamp = $date->add(DateInterval::createFromDateString('10 minutes'));
当我 运行 它时,出现以下错误:
Catchable fatal error: Object of class DateTime could not be converted to string in ...
我该如何解决?
当您调用 DateTime::add()
时,您只有一个 DateTime
对象。 您仍然需要将其转换为时间戳。使用 DateTime::getTimestamp()
to do that. You still need to convert it to a datetime value. Use DateTime::format()
来执行此操作。
$start_timestamp = $date->add(DateInterval::createFromDateString('10 minutes'))->format('Y-m-d H:i:s');
我在 php/mysql 中有以下查询:
$q2 = $conn3->prepare("SELECT (t.start_time + INTERVAL t.text_duration SECOND) as end_time FROM texts t WHERE t.start_time <= :starttime AND (t.start_time + INTERVAL t.text_duration SECOND) >= :starttime AND t.plot_id = :plot_id LIMIT 1");
$q2->bindValue(':starttime', $start_timestamp);
$q2->bindValue(':plot_id', $plot_id);
$q2->execute();
$check2 = $q2->fetch(PDO::FETCH_ASSOC);
$start_time
是一个日期时间对象,定义如下:
$date = new DateTime();
$start_timestamp = $date->add(DateInterval::createFromDateString('10 minutes'));
当我 运行 它时,出现以下错误:
Catchable fatal error: Object of class DateTime could not be converted to string in ...
我该如何解决?
当您调用 DateTime::add()
时,您只有一个 DateTime
对象。 您仍然需要将其转换为时间戳。使用 You still need to convert it to a datetime value. Use DateTime::getTimestamp()
to do that.DateTime::format()
来执行此操作。
$start_timestamp = $date->add(DateInterval::createFromDateString('10 minutes'))->format('Y-m-d H:i:s');