Date_format () expects parameter 1 to be a DateTime, a string is given

I am trying to replace my requests with a PDO request and I am having problems with date formats. I need to print the dates in the format d / m / YH: i: s, but after running the PDO script, it prints the date in this format Ymd H: i: s

while($row = $sql -> fetch(PDO::FETCH_ASSOC)) { ... echo "<td>" . date_format( $row['date'], 'd/m/YH: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>"; , then it will return to Ymd H:i:s How can I get the previous format d/m/YH:i:s ?

+6
source share
2 answers

The first parameter to date_format must be an object of the DateTime class.

 echo "<td>" . date_format( new DateTime($row['date']), 'd/m/YH:i:s' ). "</td>"; 

or alternatively

 echo "<td>" . date_format( date_create($row['date']), 'd/m/YH:i:s' ). "</td>"; 
+9
source

Change your code to the following as indicated in the PHP manual. As indicated in the error, you need to convert the value to a DateTime object before output.

  while($row = $sql -> fetch(PDO::FETCH_ASSOC)) { $date = new DateTime($row['date']); ... echo "<td>" . $date->format( $row['date'], 'd/m/YH:i:s'). "";"</td>"; ... } 
+2
source

Source: https://habr.com/ru/post/973633/


All Articles