php 警告:date_format() 期望参数 1 为 DateTime
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15567854/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Warning: date_format() expects parameter 1 to be DateTime
提问by Iain Simpson
I am using the following script to pull the calendar info out of the mysql database and display it on the page. I am trying to re format the date from the standard Mysql date format , but when retrieving it from the database am getting the following error :
我正在使用以下脚本从 mysql 数据库中提取日历信息并将其显示在页面上。我试图从标准 Mysql 日期格式重新格式化日期,但是从数据库中检索它时出现以下错误:
Warning: date_format() expects parameter 1 to be DateTime, string given in C:\easyphp\www\twinfocus\managecalendar.php on line 24
Warning: date_format() expects parameter 1 to be DateTime, string given in C:\easyphp\www\twinfocus\managecalendar.php on line 24
Warning: date_format() expects parameter 1 to be DateTime, string given in C:\easyphp\www\twinfocus\managecalendar.php on line 24
Warning: date_format() expects parameter 1 to be DateTime, string given in C:\easyphp\www\twinfocus\managecalendar.php on line 24
The Database (as you can see the dates are stored correctly):

数据库(如您所见,日期存储正确):

The script :
剧本 :
<?php
$sql2 = <<<SQL
SELECT *
FROM `calendar`
SQL;
if(!$result2 = $db->query($sql2)){ die('There was an error running the query [' . $db->error . ']');}
echo '<table class="admintable"> <thead>';
echo '<tr><th>Client Names</th><th>Email</th><th>Tel</th><th>Wedding Date</th><th>Date Created</th><th>Start Time</th><th>End Time</th><th>Price</th><th>Location</th><th>Other Info</th><th>Edit</th><th>Delete</th></tr></thead>';
while($row2 = $result2->fetch_assoc()){
$weddingdate = $row2['weddingdate'];
$formattedweddingdate = date_format($weddingdate, 'd-m-Y');
echo '<tr><td>'.$row2['name'].'</td><td>'.$row2['email'].'</td><td>'.$row2['tel'].'</td><td style="min-width:70px;">'.$formattedweddingdate.'</td><td style="min-width:70px;">'.$row2['datecreated'].'</td><td>'.$row2['starttime'].'</td><td>'.$row2['endtime'].'</td><td>£'.$row2['price'].'</td><td>'.$row2['location'].'</td><td style="min-width:400px;">'.$row2['otherinfo'].'</td><td><a href="managecalendar.php?&key='.$key.'&editwedding='.$row2['id'].'">Edit</a></td><td><a href="calenderdelete.php?&key='.$key.'&delwedding='.$row2['id'].'">Delete</a></td></tr>';}
echo '</table>';
?>
回答by Rikesh
回答by sean662
You need to pass DateTime object to this func. See manual: php
您需要将 DateTime 对象传递给此函数。参见手册: php
string date_format ( DateTime $object , string $format )
You can try using:
您可以尝试使用:
date_format (new DateTime($time), 'd-m-Y');
Or you can also use:
或者你也可以使用:
$date = date_create('2000-01-01');
echo date_format($date, 'Y-m-d H:i:s');
回答by Benz
Why don't you try it like this:
你为什么不试试这样:
$Weddingdate = new DateTime($row2['weddingdate']);
$formattedweddingdate = date_format($Weddingdate, 'd-m-Y');
Or you can also just do it like :
或者你也可以这样做:
$Weddingdate = new DateTime($row2['weddingdate']);
echo $Weddingdate->format('d-m-Y');
回答by Malith Ileperuma
try this
尝试这个
$start_date = date_create($_POST['start_date']);
$start_date = date_format($start_date,"Y-m-d H:i:s");
回答by Neeraj Agrawal
This may help
这可能有帮助
$formattedweddingdate =date('d-m-Y',strtotime($weddingdate));
回答by user3734197
I've tried sean662's 3rd solution and worked with now() function stored in an INSERT sql an then it's value in the date_create() function. After that the variable is then passed through the date_format() function and you can have the date order that you like.
我已经尝试过 sean662 的第三个解决方案,并使用存储在 INSERT sql 中的 now() 函数,然后它在 date_create() 函数中的值。之后,变量然后通过 date_format() 函数传递,您可以拥有您喜欢的日期顺序。

