Hope that title isn't too cryptic. I have an array with a DATETIME object in it and I'm just trying to figure out how to echo this to a page.
["created"]=> object(DateTime)#3 (3) { ["date"]=> string(19) "2010-10-22 00:00:00" ["timezone_type"]=> int(3) ["timezone"]=> string(13) "Europe/London"
Can someone help me out?
tried date() but get:
Warning: date() expects parameter 2 to be long, object given in C:\
any help most appreciated,
Jonesy
Use DateTime::format()
. The mask syntax is identical to date()
's.
echo $value->format('Y-m-d H:i:s');
I add this answer even if I am not sure it answers specifically the question (and best answer is already there though),
but I couldn't find much other places where the above format (date
/timezone_type
/timezone
) is mentioned.
If you have the date translated from object to array
var_export
DateTime::__set_state(array(
'date' => '2017-12-05 11:58:25.428595',
'timezone_type' => 3,
'timezone' => 'US/Pacific',
))
json_encode
{"date":"2017-12-05 11:57:07.938671","timezone_type":3,"timezone":"US\/Pacific"}
you can use again the DateTime::__set_state
magic method mentioned above to convert it again to a DateTime object.
Not sure how here __set_state
could bidirectional, but it does the magic.
I couldn't find documentation.
But you can test it here: http://sandbox.onlinephpfunctions.com/code/0a18e6937e7d4373beb91713f2e6e5f75f9af3e2
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With