Skip to content
Advertisement

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.

Advertisement

Answer

Just backslash the T

<?php

$date = new DateTime('2019-12-10 14:00');
echo $date->format('Y-m-dTH:i:s');

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

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement