Q8root
Q8root

Reputation: 1385

Changing the date format in laravel

I am using date picker from Bootstrap template framework in one of my Insert record view file, in order to DB accepting the date format i need to convert it to another format using :-

$this->formatDate($request->date)

Now i am creating an edit view file , so in that edit new file i want to set the data picker value to ="$this->date" that will set the picker value to the same DB record date, it return error :

(Invalid date)

How can i return back to the same original date picker format so i can view it?

Upvotes: 0

Views: 257

Answers (3)

Raza Mehdi
Raza Mehdi

Reputation: 941

Both answers provided by Pyae & Sidharth are right, however i would simply define a mutator in my model to set this to a format i like:

public function setRequestDateAttribute($value)
{
    $this->attributes['request_date'] = \Carbon\Carbon::parse($value)->format('d/m/Y');
}

Upvotes: 1

Pyae Sone
Pyae Sone

Reputation: 1624

In Laravel, we can use Carbon.

{{ \Carbon\Carbon::parse($request->date)->format('d/m/Y')}}

It provides an easier way to change any format. Go to see documentation about Laravel Carbon

Upvotes: 0

Sidharth
Sidharth

Reputation: 1925

Use Laravel's dependency for date manipulation called Carbon.

You need to require it in a class to use it.

use Carbon\Carbon;

then you can use the Carbon class to parse and format dates in the following method;

$date = Carbon::parse($request->date)

This can be saved to the model without any manipulation.

You can have a look at the tutorial video here to learn more.

Official documentation is available here

Upvotes: 0

Related Questions