Skip to main content

Convert ISO8601 date to MYSQL date time using Carbon in PHP / Laravel

I was working on a project using VueJS and was using this datepicker which is a great vue plugin.

However, when sending the date to the database in Laravel, it posts as ISO8601 format (eg. 1996-10-15T00:05:32.000Z)

MySQL doesn’t like that format, so we need to convert it to the correct date time format so MYSQL can store it.

To do it, we use Carbon. Carbon comes with Laravel which is awesome, so we can just import Carbon into our controller and make it work.

use Illuminate\Support\Carbon;
use App\Model;

public function save(Request $request)
{   

     $model = new Model;
     $model->date = Carbon::parse($request->date)->setTimezone( 'Australia/Sydney' )->format('Y-m-d h:i:s');
     $model->save();
}

Leave a Reply