Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Warning: date_format() expects parameter 1 to be DateTime

Tags:

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 :

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): enter image description here

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>&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>';  ?> 
like image 585
Iain Simpson Avatar asked Mar 22 '13 10:03

Iain Simpson


2 Answers

Best way is use DateTime object to convert your date.

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

Note: It will support for PHP 5 >= 5.3.0 only.

like image 163
Rikesh Avatar answered Oct 21 '22 10:10

Rikesh


You need to pass DateTime object to this func. See manual: 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'); 
like image 34
sean662 Avatar answered Oct 21 '22 11:10

sean662