Reputation: 1024
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
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
Reputation: 3117
This should work :
$datetime = DateTime::createFromFormat('Y-m-d H:i:s', '2014-02-15 14:55:29');
Upvotes: 1