gpt4 book ai didi

php - 警告 : date_format() expects parameter 1 to be DateTime

转载 作者:IT王子 更新时间:2023-10-29 00:09:52 26 4
gpt4 key购买 nike

我正在使用以下脚本从 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

数据库(如您所见,日期已正确存储): enter image description here

脚本:

<?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>&pound;'.$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反对转换您的日期。

$myDateTime = DateTime::createFromFormat('Y-m-d', $weddingdate);
$formattedweddingdate = $myDateTime->format('d-m-Y');

注意:它将仅支持 PHP 5 >= 5.3.0。

关于php - 警告 : date_format() expects parameter 1 to be DateTime,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15567854/

26 4 0