0

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
2
  • Please provide some code. Commented Feb 15, 2014 at 15:10
  • If you show the SQL query, you can probably do the date diff in the database. Commented Feb 15, 2014 at 15:20

2 Answers 2

2

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);
Sign up to request clarification or add additional context in comments.

Comments

1

This should work :

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

1 Comment

createFromFormat is unecessary as that date format is readable by DateTime()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.