12

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

1
  • echo ((array)myArray["created"])['date']; or u can ->format(DateTime::DATE_FORMAT_CONST); Commented Mar 13, 2018 at 8:01

4 Answers 4

12

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

Sign up to request clarification or add additional context in comments.

2 Comments

off topic but usefull.
Documentation is here, but not yet complete php.net/manual/en/datetime.set-state.php
11

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

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

Comments

5

http://www.php.net/manual/en/datetime.format.php

echo date_format(myArray["created"], "the format you want for your date");

Formatting:

http://www.php.net/manual/en/function.date.php

Comments

1

If it's an actual php5 DateTime object then you can use the format method to echo it

$myDate = $myArray['created'];
echo $myDate->format('Y-m-d H:i:s');

Comments

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.