[英]date_format() expects parameter 1 to be DateTime, string given
I'm trying to replace my queries to PDO query and I have problems with date formats. 我正在尝试将我的查询替换为PDO查询,我遇到日期格式问题。 I need to print dates in format d/m/YH:i:s but after PDO script runs it prints the date in this format Ymd H:i:s
我需要以d / m / YH格式打印日期:i:s但是在PDO脚本运行之后它以这种格式打印日期Ymd H:i:s
while($row = $sql -> fetch(PDO::FETCH_ASSOC))
{
...
echo "<td>" . date_format( $row['date'], 'd/m/Y H:i:s'). "";"</td>";
...
}
Warning: date_format() expects parameter 1 to be DateTime, string given in
But if I change the code to echo "<td>" . $row['date']. "";"</td>";
但是,如果我更改代码以
echo "<td>" . $row['date']. "";"</td>";
echo "<td>" . $row['date']. "";"</td>";
then it returns to Ymd H:i:s
How can I get the previous format d/m/YH:i:s
? 然后它返回到
Ymd H:i:s
我怎样才能得到之前的格式d/m/YH:i:s
?
The first parameter to date_format
needs to be an object of DateTime
class. date_format
的第一个参数需要是DateTime
类的对象。
echo "<td>" . date_format( new DateTime($row['date']), 'd/m/Y H:i:s' ). "</td>";
or, alternatively 或者,或者
echo "<td>" . date_format( date_create($row['date']), 'd/m/Y H:i:s' ). "</td>";
Change your code to the following as provided in the PHP manual. 将代码更改为PHP手册中提供的以下内容。 As stated in the error you need to convert the value to DateTime object before outputting.
如错误中所述,您需要在输出之前将值转换为DateTime对象。
while($row = $sql -> fetch(PDO::FETCH_ASSOC))
{
$date = new DateTime($row['date']);
...
echo "<td>" . $date->format( $row['date'], 'd/m/Y H:i:s'). "";"</td>";
...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.