Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP: DATETIME in array as object. How to echo

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

like image 695
iamjonesy Avatar asked Oct 25 '10 16:10

iamjonesy


2 Answers

Use DateTime::format(). The mask syntax is identical to date()'s.

echo $value->format('Y-m-d H:i:s');
like image 50
Pekka Avatar answered Sep 17 '22 20:09

Pekka


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

  • via var_export

DateTime::__set_state(array( 'date' => '2017-12-05 11:58:25.428595', 'timezone_type' => 3, 'timezone' => 'US/Pacific', ))

  • or 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

like image 23
Kamafeather Avatar answered Sep 19 '22 20:09

Kamafeather