Php 警告:date_format()要求参数1为DateTime
我使用下面的脚本从mysql数据库中提取日历信息并显示在页面上。我正在尝试从标准Mysql日期格式重新格式化日期,但从数据库检索时出现以下错误:Php 警告:date_format()要求参数1为DateTime,php,date,Php,Date,我使用下面的脚本从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
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
数据库(如您所见,日期存储正确):
剧本:
<?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>';
?>
您需要将DateTime对象传递给此函数。 参见手册: 您可以尝试使用:
date_format (new DateTime($time), 'd-m-Y');
或者,您也可以使用:
$date = date_create('2000-01-01');
echo date_format($date, 'Y-m-d H:i:s');
你为什么不这样试试呢:
$Weddingdate = new DateTime($row2['weddingdate']);
$formattedweddingdate = date_format($Weddingdate, 'd-m-Y');
或者你也可以这样做:
$Weddingdate = new DateTime($row2['weddingdate']);
echo $Weddingdate->format('d-m-Y');
最好的方法是使用对象转换日期
$myDateTime = DateTime::createFromFormat('Y-m-d', $weddingdate);
$formattedweddingdate = $myDateTime->format('d-m-Y');
注意:它只支持PHP5>=5.3.0。我尝试了sean662的第三个解决方案,并使用了存储在INSERT sql中的now()函数,然后在date\u create()函数中使用它的值。之后,变量将通过date_format()函数传递,您可以获得自己喜欢的日期顺序。试试这个
$start_date = date_create($_POST['start_date']);
$start_date = date_format($start_date,"Y-m-d H:i:s");
这可能会有帮助
$formattedweddingdate =date('d-m-Y',strtotime($weddingdate));
日期($weddingdate,'d-m-Y')@Pramod你的参数是错误的…我认为人们看到警告时会惊慌失措,不去读它。警告说该函数需要
DateTime
的实例,但您已经传递了string
,因此您的变量$weddingdate
包含日期字符串,所以您只需创建DateTime
的对象(例如date\u格式(新的DateTime($weddingdate),$desiredFormat)
)谢谢!这正在工作。谢谢,现在就可以了!:-)我不知道这是什么,因为“太本地化了”-它肯定解决了我的问题。这个答案在互联网上的其他几个答案给我错误的地方起作用,关键是告诉DateTime我所拥有的日期字符串的格式。请提供一些上下文或详细信息来回答,以解释为什么这样可以解决问题,从而帮助其他人。strotime()函数将英文文本datetime解析为Unix时间戳(自1970年1月1日00:00:00 GMT以来的秒数),并使用date()函数对其进行格式化。
$formattedweddingdate =date('d-m-Y',strtotime($weddingdate));