Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

bendel's avatar

Eloquent date to int.

How to convert eloquent DateTime attributes to int?

0 likes
4 replies
toniperic's avatar

@IFGroup how would that work? When creating "2015-4-2 12:00:00" to integer, what should be returned?

1 like
rodrigo.pedra's avatar
Level 56

I will assume that by converting to int you mean getting the unix timestamp.

$timestamp = $user->created_at->timestamp

created_at and updated_at are automatically converted to Carbon instances by Eloquent. See Carbon as a DateTime on steroids [ https://github.com/briannesbitt/Carbon ].

If you need other fields in your model to be treated like that by Eloquent take a look in the docs [ http://laravel.com/docs/5.0/eloquent#date-mutators ]. Basically you should override the getDates(...) method

public function getDates()
{
    return ['created_at', 'my_date'];
}
2 likes
nguuuks's avatar

Hi thanks for explanation.

I have tried this way, i got an integer output, but not like the number of date. It's output be 1607513629, not like a date number.

Snapey's avatar

you need to explain better what you want

Please or to participate in this conversation.