将 Carbon 日期转换为 html 日期时间格式

Convert Carbon date to html date time format

我有这个格式来自 Carbon

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

现在我想转换此日期时间格式以在 datatime-local 输入中使用它。

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

但它需要另一种格式。

这里说 https://developer.mozilla.org/en-US/docs/Web/HTML/Date_and_time_formats#Local_date_and_time_strings :

" 有效的本地日期时间字符串由日期字符串和时间字符串组成,并用字母 "T" 或 space 字符将它们连接在一起。没有关于时区的信息包含在字符串中;日期和时间假定为用户的本地时区。"

但我不知道如何将碳格式化为这个。

只需反斜杠 T

<?php

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

输出 2019-12-10T14:00:00,在此处检查 https://3v4l.org/PTAeZ

您可以简单地使用日期函数 n PHP

$datetime = '2019-12-10 14:00';

print_r(date('Y-m-d \T H:i:s', strtotime($datetime)));