Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Object of class Carbon\Carbon could not be converted to int

I have already search for this, and i did find some topics discussing this problem, however none of which I felt like was the way I want to go, so therefore I hope to get a better sugestion here.

I want to get a timestamp from when a user was last updated.

$user->updated_at

If i run this, it gives me this error

Object of class Carbon\Carbon could not be converted to int

This is because it returns a Carbon instance in Laravel 5.2, and I only need the timestamp.

Carbon {#303 ▼
  +"date": "2017-01-04 00:35:38.000000"
  +"timezone_type": 3
  +"timezone": "UTC"
}

How do i fetch the "date" from the carbon instance, so I only get 2017-01-04 00:35:38 - I have no idea where .000000 comes from, as this doesn't show up in the database. In the MySQL database, it simply shows 2016-07-20 20:23:07 as of type timestamp.

Hope for some pointers and suggestions

like image 411
Patrick Avatar asked Jan 04 '17 01:01

Patrick


1 Answers

The Carbon object has a toDateTimeString method you can use:

$user->updated_at->toDateTimeString();

It will give you the value 2017-01-04 00:35:38 as a string.

like image 193
Dekel Avatar answered Oct 27 '22 22:10

Dekel