TotalNewbie
TotalNewbie

Reputation: 1024

PHP SQL Datetime issue

I have a DATETIME field within a SQL table and retrieve the data accordingly - when trying to use the date_diff function however I receive the following message:

Message: date_diff() expects parameter 1 to be DateTime, string given

is there a way to convert the string I have taken from the SQL DB back into a date/time, the field format is as follows:

Y    -M -D H -M -S   
2014-02-15 14:55:29

Upvotes: 0

Views: 51

Answers (2)

John Conde
John Conde

Reputation: 219894

You are passing the datetime string to date_diff() but that function expects a DateTime() object. You need to create a DateTime() object with the date first, then use date_diff().

$date1 = new DateTime('2014-02-15 14:55:29');
$date2 = new DateTime();
$interval = $date1->diff($date2);

Upvotes: 2

mehmetseckin
mehmetseckin

Reputation: 3117

This should work :

$datetime = DateTime::createFromFormat('Y-m-d H:i:s', '2014-02-15 14:55:29');

Upvotes: 1

Related Questions