DateTime Object to string

When I pull birthday date from the Facebook SKD I get DateTime Object, and I have been struggling convert it to string.

print_r($birthday);

DateTime Object ( [date] => 1978-03-09 00:00:00.000000 [timezone_type] => 3 [timezone] => Europe/Berlin )

But when i try to create another DateTime Object and convert it over to string

$date = new DateTime($birthday->date);
$result = $date->format('Y-m-d H:i:s');

I get

Undefined prperty: DateTime::$date

And when I

$date = new DateTime($birthday['date']);
$result = $date->format('Y-m-d H:i:s');

Cannot use object of type DateTime as array

Upvotes: 6

Views: 24901

Answers (1)

RiggsFolly
RiggsFolly

Reputation: 94642

Why try and create a new DateTime object use the one you have and just format the output

echo $birthday->format('Y-m-d H:i:s');

Upvotes: 15

Related Questions