Search code examples
phplaravelphp-carbon

Convert Carbon date to html date time format


I have this format comes from Carbon

 // Y-m-d H:i
$datetime = 2019-12-10 14:00;

Now i want to convert this date time format to use it in datatime-local input.

<input type="datetime-local" value="{{ $datetime }}">

But it needs another format.

It says here https://developer.mozilla.org/en-US/docs/Web/HTML/Date_and_time_formats#Local_date_and_time_strings :

" A valid datetime-local string consists of a date string and a time string concatenated together with either the letter "T" or a space character separating them. No information about the time zone is included in the string; the date and time is presumed to be in the user's local time zone."

But i don't know how to format the carbon to this.


Solution

  • Just backslash the T

    <?php
    
    $date = new DateTime('2019-12-10 14:00');
    echo $date->format('Y-m-d\TH:i:s');
    

    Outputs 2019-12-10T14:00:00, check it here https://3v4l.org/PTAeZ