Skip to main content
Category:

Code snippet that can be used to convert input date format before saving model in Laravel 7.

 

Using Date Mutators:

You can actually have laravel automatically mutate your dates to a Carbon object by adding the dates array to your model:

protected $dates = [
    'event_date'
];
 

Customize Date Formats:

    /**

     * If you need to customize the format of your timestamp

     * columns "created_at" and "updated_at"

     * set the $dateFormat property on your model.

     * examples:

     * 'U' - timestamp (seconds from 01.01.1970)

     * 'Y-m-d H:i:s' - e.g. 1985-12-25 15:01:24

     * etc.

     *

     * @var string

     */

    protected $dateFormat = 'U';

 

Also you can Use Carbon to convert TO the d-m-Y format date.

Example:

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

$user->save();

 

Riadh Rahmi

Senior Web Developer PHP/Drupal & Laravel

I am a senior web developer, I have experience in planning and developing large scale dynamic web solutions especially in Drupal & Laravel.

Web Posts

Search

Page Facebook