In my project I used this code to format timestamps fields
date('D m Y H:i', strtotime($post->created_at))
But as I have many places and fields to display it's a bit boring, and if I need to change the format it won"t be easy to maintain.
I'd like to know if there is a way to decalre the output format
You can create an accessor function in your Post model.
public function getCreatedAtAttribute($value)
{
return date('D m Y H:i', strtotime($value));
}
This way, each time you'll call $post->created_at
it will display the date returned by your accessor instead of the default value.
More info here : http://laravel.com/docs/eloquent#accessors-and-mutators
If you don't want this function in all your models your can create a BaseModel class and make your models extend this BaseModel class.
I also found an other solution:
\Carbon\Carbon::setToStringFormat('D m Y H:i');
You can use this line (in global.php for exemple) but it will change the date format in all the application.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With