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

pimski's avatar
Level 13

Best way to juggle with date formats?

Hi there,

I have a Model that has a date attribute (let's call it date :). In the database this is stored as "yyyy-mm-dd". However, in the front-end I want to display the date (and use it in forms) as "dd-mm-yyyy". What's the best way to handle dates?

So far I've found:

  1. Laravel can automatically convert dates to Carbon objects using the $dates array from the HasAttributes trait
  2. You can create a setAttribute() mutator function to process an incoming value before it is set on the $attributes array
  3. If you want to display the date (or use it in a form) you can use the Carbon/DateTime functions

Piece of model code:

protected $dates = ['date'];

public function setDateAttribute($value)
{
    if ($value instanceof Carbon) {
        $this->attributes['date'] = $value;
    } elseif ($value) {
        $this->attributes['date'] = Carbon::createFromFormat('d-m-Y', $value);
    }
}
0 likes
3 replies
5pArxz's avatar

Dear @pimski perhaps you could make use of Laravel getAttribute as well ?

Something like this:


public function getDateAttribute($date)
{
    return Carbon::parse($date)->format('d-m-Y');
}

And in your views if you call $entity->date you will get the desired result ?

Alternatively if you would like more control and flexibility on how you retrieve the date, perhaps you could try this:

  1. Create a dedicated DateFormatter like this:
<?php

namespace App\Formatters;

use Carbon\Carbon;

class DateFormatter
{
    /**
     * Carbon $date
     */
    private $date;

    /**
     * Inject Date from Entity
     */
    public function __construct($date)
    {
        $this->date = Carbon::parse($date);
    }

    /**
     * Format Date for Form Use
     */
    public function forForm()
    {
        return $this->date->format('d-m-Y');
    }

    /**
     * Format date for Profile
     */
    public function forHumans()
    {
        return $this->date->diffForHumans();
    }
    
}

And then on your Entity you could change the getDateAttribute method to something like this:


public function getDateAttribute($date)
{
    return new DateFormatter($date);
}

This will allow you to format the dates in many different ways the view could require by simply calling:

{{ $entity->date->forForm() }}

Here is a video here on Laracasts that explains this method in more detail: https://laracasts.com/series/whip-monstrous-code-into-shape/episodes/6

Kindly note the above stated code was not tested, and it's only to serve as an idea :)

2 likes
pimski's avatar
Level 13

Hi @5pArxz thanks for your reply!

This is definitely usefull for displaying dates. However, I was wondering if my strategy of collecting/converting dates from a user is the way to go. Or whether there's a cleaner method...

5pArxz's avatar
5pArxz
Best Answer
Level 16

@pimski When it comes to setting dates i use almost the exact method you are, i've yet to investigate a better method, as it's so simple i write it exactly once on my Entities, could always extract the setter to a trait and import them across the board for all your entities and perhaps even use the protected $dates [] to determine which dates must be parsed before saving to database as Carbon::parse() can accept dates in many different ways, there should almost never be a issue.

For retrieving dates, i generally use a general DateFormatter VO as the one above. Or for throw away apps i just yolo and {{ $entity->date->format('d-m-Y') }}

Please or to participate in this conversation.