John Knutson
John Knutson

Reputation: 124

How to extract the date string from a date object in PHP

I need to extract the date string from a mysql updated_at date object. A var_dump() shows this:

 ["date"]=>
    object(Carbon\Carbon)#235 (3) {
      ["date"]=>
      string(26) "2016-04-26 16:41:05.000000"
      ["timezone_type"]=>
      int(3)
      ["timezone"]=>
      string(3) "UTC"
    }

My code looks like this:

$posts[] = array(
'id' => $post->id,
'text' => $post->text,
'category' => $category,
'image' => $post->image,
'date' => $post->updated_at
);

But after I've converted it to JSON, I end up with this:

"date":{"date":"2016-04-12 23:49:41.000000","timezone_type":3,"timezone":"UTC"}}

How do I get at that inner date?

Upvotes: 0

Views: 523

Answers (1)

Nicolo
Nicolo

Reputation: 61

Since date it's still an object you've to assign the inner property like this:

 $posts[] = array(
'id' => $post->id,
'text' => $post->text,
'category' => $category,
'image' => $post->image,
'date' => $post->updated_at->date
);

Upvotes: 1

Related Questions