Setting and Formating Time Date Laravel

Published 6/6/2024

// app/config.php "timezone" => "America/New_York" Make it configurable For all the examples above, we need to replace the exact formats with values from config. So, let’s add these variables to config/app.php:


return [
  'date_format' => 'm/d/Y',
  'date_format_javascript' => 'MM/DD/YYYY',
];
And then, in Laravel, we can use this:

Carbon::createFromFormat(config('app.date_format'), $value)->format('Y-m-d');
Carbon::parse($value)->format(config('app.date_format'));
Finally, we can’t insert variable inside JavaScript, but we can add that script command inside of Blade file, and then we can do this:

  $('.date').datetimepicker({
    format: '{{ config('app.date_format_javascript') }}',
    locale: 'en'
  });
Now, as a full result, if we need to change date format in all project, all we need to do is change variables in config/app.php file.