Support the ongoing development of Laravel.io →
posted 11 years ago
Views Packages

My dateTime fields are stored in mySQL as a timestamp and the fields are mutated? as below:

public function getDates()
{
    /* substitute your list of fields you want to be auto-converted to timestamps here: */
    return array('created_at', 'updated_at', 'start', 'end');
}

I can format in my view nicely now using ->format(' ...'); but in my view I need to show the difference between two dateTimes.

Last updated 3 years ago.
0

Try something like

public function getDurationAttribute()
{
    return $this->end->diffForHumans($this->start);
}

Then just access it using :

$model->duration;

Beware of some edge cases (this code will fail if end is null).

Also you can use the Carbon documentation about dates comparaisons and differences:

https://github.com/briannesbitt/Carbon#api-comparison

https://github.com/briannesbitt/Carbon#difference

Last updated 3 years ago.
0

Thanks atrakeur that was right on the money!

Last updated 3 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

netm netm Joined 2 Apr 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.