简体   繁体   English

date_format()期望参数1为DateTime,给定字符串

[英]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.

相关问题 警告:date_format() 期望参数 1 为 DateTime - Warning: date_format() expects parameter 1 to be DateTime date_format() 期望参数 1 为 DateTimeInterface,给定布尔值 - date_format() expects parameter 1 to be DateTimeInterface, boolean given PHP date_format()期望参数1为DateTimeInterface - PHP date_format() expects parameter 1 to be DateTimeInterface DateTime格式期望参数1为字符串,给定对象-Symfony2 - DateTime format expects parameter 1 to be string, Object given - Symfony2 date_add()期望参数1为DateTime,给定字符串 - date_add() expects parameter 1 to be DateTime, string given date_format() 期望参数 1 为 DateTimeInterface,给定 null(视图:/home/vagrant/code/blog/resources/views/singlePost.blade.php) - date_format() expects parameter 1 to be DateTimeInterface, null given (View: /home/vagrant/code/blog/resources/views/singlePost.blade.php) DateTime :: __ construct()期望参数1是字符串,给定对象 - DateTime::__construct() expects parameter 1 to be string, object given DateTime :: diff()期望参数1为DateTimeInterface,给定字符串 - DateTime::diff() expects parameter 1 to be DateTimeInterface, string given DateTime :: createFromFormat()期望参数2为字符串,给定对象 - DateTime::createFromFormat() expects parameter 2 to be string, object given 警告:date()期望参数2为long,字符串为 - Warning: date() expects parameter 2 to be long, string given in
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM