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

saieesh's avatar

Convert String date of date

In MySQL table I have one field "Created_at" which have datatype varchar & the date is stored in this format "31072021". I want to convert string date to datetime without change the datatype in my table. I tried to use protected $casts = ['Created_at' => 'datetime:Y-m-d']; In the model Also I tried to use withCasts([Created_at' => datetime:Y-m-d]) but I'm not able to convert string to datetime ( like 31072021 => 2021-07-31 ). Can anyone tell me how to do this?

0 likes
1 reply
Sinnbeck's avatar

What is 31072021 representing. Days since x? What date?

And you can use a mutator

public function getCreatedAtAttribute($value)
{
     return $value;//calculate date based on value
}

Please or to participate in this conversation.