Comparing two timestamps in Laravel

For the date fields in the project we use the timestamps datatype. In Laravel usually we have created_at and updated_at fields as default when a table is created.You can easily change the field names if you want. Suppose you have changed the name of updated_at field name to edited_at. Then if you var_dump(edited_at) it will give string and var_dump(created_at) will give object/Carbon. So if you want to compare both timestamps you have to first change the edited_at timestamp into carbon then you can compare both easily. For changing edited_at timestamp to carbon object specify it in your model.

protected $dates = [‘edited_at’];

The following are the comparison functions of Carbon.

eq()           - equals

ne()           - not equals

gt()           - greater than

gte()         - greater than or equals

lt()            - less than

lte()          - less than or equals


if($model-> edited_at->gt($model->created_at)){

// edited at is newer than created at