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

User1980's avatar

Casting date issues

Hello,

I did not deal with dates for some time and totally forgot how it works. I have a view app that sends the data as 'DD-MM-YYYY', it looks like the date column in MySQL must receive the data as 'YYYY-MM-DD'

Could you please remind me the best way to cast the $request->date to MySQL format but also output the MySQL back to DD-MM-YYYY each time I json response my collections back to vue.

Thanks!

0 likes
2 replies
Nakov's avatar
Nakov
Best Answer
Level 73

This will do it:

Carbon::createFromFormat('d-m-Y', $request->date);

or add accessor and mutator:

public function setDateAttribute($value)
{
		$this->attributes['date'] = Carbon::createFromFormat('d-m-Y', $value);
}

public function getDateAttribute()
{
		return $this->date->format('d-m-Y') ?? null;
}
1 like
User1980's avatar

@Nakov Thank you so much,

Please update your reply with this as this is what worked for me:

$date = Carbon::createFromFormat('d-m-Y', $request->birthday)->format('Y-m-d');

The d-m-Y was coming from vue js, but MySQL date column only accepts Y-m-d

Thanks again! I needed a little refresh as I had lost my snippet for this.

Please or to participate in this conversation.