Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DateTime Object to string

Tags:

php

datetime

time

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

like image 624
Aðalsteinn Leifsson Avatar asked Dec 15 '22 09:12

Aðalsteinn Leifsson


1 Answers

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');
like image 124
RiggsFolly Avatar answered Dec 25 '22 11:12

RiggsFolly