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

uxweb's avatar
Level 20

Saving dates nor formatted as Y-m-d

Hi friends!,

I am working in a project with L4 which uses several tables which have date fields.

The problem is that the dates coming to the models are not in the Y-m-d format, i use d-m-Y format in the forms for language reasons.

When i pass the date string coming from the form to a model it crashes.

I have tried to use mutators to format the date as Y-m-d and worked, but it is overwhelming have to create for each model several mutators for the date fields.

Other option i have tried is to create carbon objects from this string dates before passing them to the model, this works, but i have always to do that several times when i want to create or update a model.

Do you know a better way to handle this or any of both i have used is the best?

Thank you!

0 likes
8 replies
yadakhov's avatar

Use Carbon to convert the d-m-Y format date.

Example:

$user->date_column = Carbon::createFromFormat('d-m-Y', $youdate);

$user->save();

sldobri@live.com's avatar

public function getCreatedAtAttribute( $value ) { return Carbon::parse( $value )->format( 'd/m/Y H:i:s' ); }

uxweb's avatar
Level 20

@yadakhov yep, i have done that but still not sure if that is the more elegant way of doing it, dou you have more ideas?

uxweb's avatar
Level 20

@sldobri returning the dates is ok, they work as expected already, it is just when creating and updating the date fields.

Thanks!

sitesense's avatar

I would suggest extending a base model.

Luckily I did a quick search and found this, so no need to write it all up.

https://coderwall.com/p/ms-a1g/change-created_at-display-format-globally-in-laravel-4

I know Jeffrey doesn't like linking out to sites without including the solution, so here it is:

<?php
// BaseModel.php

use Carbon\Carbon;

class BaseModel extends Eloquent {

    public function getCreatedAtAttribute($attr) { 
        return Carbon::parse($attr)->format('d-m-Y'); //Change the format to whichever you desire
    }
}
<?php
// Example.php

class Example extends BaseModel {
    protected $table = 'example';
}

"Now all your created_at timestamps can be formatted globally throughout your application by simply extending your model to the BaseModel."

1 like
uxweb's avatar
Level 20

Wow, nice option, but it is related to display the dates, what I mean is when saving or updating those date fields.

sitesense's avatar

I'm really new to Laravel and only playing around with it at the moment.

Waiting for L5 before I dig much deeper, but from what I understand, you should be able to do something like this in your base model:

    /**
     * Listen for save event
     */
    protected static function boot()
    {
        parent::boot();

        static::saving(function($model)
        {

            $model->yourDateColumn = Carbon::createFromFormat('d-m-Y', $model->yourDateColumn);

            return $model;

        });
    }

Could be totally wrong so don't grill me if that's bad lol.

bestmomo's avatar

Create event is a solution but maybe it's simplier to extend the save method of the Model :

public function save()
{
      $model->created_at = Carbon::createFromFormat('d-m-Y', $model->created_at);
      parent::save();
}

in a BaseModel or a trait if for many models.

Please or to participate in this conversation.