Amrinder Singh
Amrinder Singh

Reputation: 5482

Can't get value from DateTime object in PHP

I am facing an issue while trying to get value from an object in PHP. Here is the object:

DateTime Object
(
    [date] => 2017-06-03 18:33:48.000000
    [timezone_type] => 1
    [timezone] => +00:00
)

it's all in this variable $call->dateCreated

and here is how I am trying to get the value of "date" from the object:

$call->dateCreated->date;

but when I try to do this, I got an error:

Undefined property: DateTime::$date

Upvotes: 0

Views: 2276

Answers (3)

For date type objects, you can not access that value, you can access it through the richness of an expression by following a format.

I'll explain:

$call->dateCreated->date // It is not accessible

But if you ask the data in this form you will get the result

$call->dateCreated->format('Y-m-d H:i:s');

Upvotes: 1

Mohammad Hamedani
Mohammad Hamedani

Reputation: 3354

It's a DateTime object, so you can get wanted format date by calling format() function:

$call->dateCreated->format('Y-m-d H:i:s');

Upvotes: 6

zenwraight
zenwraight

Reputation: 2000

This should fix it, try something like this

$date_array = (array) $call->dateCteated;

Then use the date value as

$date_array['date']

Hope this helps!

Upvotes: -1

Related Questions