Search code examples
phplaraveldatetimephp-carbon

Convert AM/PM to 24 hours clock in Laravel input field


Problem: I'm using laravel forms in combination with Carbon to try and pass a date value to my database. I'm trying to switch the input field format to 24 hours instead of AM/PM, but I keep getting the standard AM/PM format. I tried ->format() but didn't find the correct solution.

Question: How can I replace the AM / PM Format of the input field to the 24 hours clock?

My code:

{{Form::date('date', \Carbon\Carbon::now(),['class' => 'form-control'])}}
{{Form::time('time', \Carbon\Carbon::now()->timezone('Europe/Brussels'),['class' => 'form-control'])}}

// Input field values:

// Date: 08/16/2018 , Time: 07:47 PM -> 19:47:00

Input result when changing the format:

{{Form::time('time', \Carbon\Carbon::now()->timezone('Europe/Brussels')->format('H:i:s'),['class' => 'form-control'])}}

// Input value of time:
// Time: 08:00:29 PM 

Solution

  • You'll need to change your time field to the following

    {{Form::time('time', \Carbon\Carbon::now()->timezone('Europe/Brussels')->format('H:i:s'),['class' => 'form-control'])}}
    

    To explain the formatting;
    H - 24 Hour with trailing zeros
    i - Minutes with trailing zeros
    s - Seconds with trailing zeros

    You can see all the date/time variables at PHP Date