如何为输入类型'datetime-local'设置一个值?

How to set a value for the input type 'datetime-local'?

我试过这个:

<input type="datetime-local"  value="<?php echo $row['Time']; ?>" class="date" name="start" REQUIRED>

如何使用数据库中的数据设置此输入字段的值?
没用!!
我也需要让编辑成为可能。
或者我应该使用其他类型的输入?
$row['Time']来自数据库!

我不知道 $row['Time'] 中到底是什么,但应该如下所示:

定义

A valid date-time as defined in RFC 3339 with these additional qualifications:

  • the literal letters T and Z in the date/time syntax must always be uppercase
  • the date-fullyear production is instead defined as four or more digits representing a number greater than 0

Examples

  • 1990-12-31T23:59:60Z
  • 1996-12-19T16:39:57-08:00

解决方案

要在 PHP 中创建 RFC 3339 格式,您可以使用:

echo date('Y-m-d\TH:i:sP', $row['Time']);

或换一种方式:

echo date("c", strtotime($row['Time']));  

或者如果您喜欢 objective 风格:

echo (new DateTime($row['Time']))->format('c');

在你的代码中

所以在你的代码中它看起来如下:

<input type="datetime-local"  value="<?php echo date('Y-m-d\TH:i:sP', $row['Time']); ?>" class="date" name="start" REQUIRED>

<input type="datetime-local"  value="<?php echo date("c", strtotime($row['Time'])); ?>" class="date" name="start" REQUIRED>

手动

这很简单,而且对我有用 首先将您的 php 值转换为这种格式

 <?php  $datetime = new DateTime($timeinout[0]->time_in);   ?>

然后在 html 输入元素的值中 使用这种格式

<input type="datetime-local" id="txt_time_in" placeholder="Time In" name="timein" value = "<?php echo $datetime->format('Y-m-d\TH:i:s'); ?>" class="form-control" /> 

这会将您的值设置为输入元素

您可以使用

date('Y-m-d\TH:i'); //Example result: '2017-01-01T01:01'

如果使用 \T 而不是 T(不工作)

date('Y-m-dTH:i'); //Example result: '2017-01-01UTC01:01'
$tripid=$_REQUEST['tripid'];
$sql="SELECT * FROM tripdetails WHERE trip_id='".$tripid."'";
$trpresult=mysqli_query($connect,$sql);
  if(mysqli_num_rows($trpresult)==1)
    {
      $trpdetails=mysqli_fetch_assoc($trpresult);
    }
 $trpstartdate = substr_replace($trpdetails['trip_start_date'],T,11,0);

 $string = preg_replace('/\s+/', '', $trpstartdate);

This is Html part

<input type="datetime-local" name="trip_start_date" id="cal" value="<?php echo $string?>">

Karol Gasienica 的回答是一个很好的解释,但不知何故即使在他们的回复中也对我不起作用

date('Y-m-d\TH:i:s', $row['Time']); //Gives me 1970-01-01 00:00
date('Y-m-d\TH:i:sP', $row['Time']); //Gives me no display
date("c", strtotime($row['Time'])); //No display too

对我有用的是这个

$t = $row['Time'];
date('Y-m-d\TH:i:s', strtotime($t)); // This got it perfectly

但是由于解释,我仍然投了赞成票。

使用 <input type="datetime-local">

提交 <form>

您将获得的值格式如下所示。

2019-09-06T00:21

在您的输入类型框中设置新值。

您必须使用:

date('Y-m-d\TH:i', strtotime($exampleDate)) //2019-08-18T00:00

解决方案示例:

$exampleDate = "2019-08-18 00:00:00"; //sql timestamp
$exampleDate = strtotime($exampleDate); //convert to unix timestamp 
$newDate = date('Y-m-d\TH:i', $exampleDate); //format unix to date

$exampleDate = "2019-08-18 00:00:00";//sql timestamp
$newDate = date('Y-m-d\TH:i', strtotime($exampleDate));

如果你不使用strtotime()你会得到一个错误

Notice: A non well formed numeric value encountered


**测试**:
 - $exampleDate = 2019-08-18 00:00:00 ;
 - //Not Working - output(1970-01-01T01:33:39)
 - <?php echo date('Y-m-d\TH:i:s', $exampleDate);?>
 - //Not Working - output(1970-01-01T01:33:39+01:00)
 - <?php echo date('Y-m-d\TH:i:sP', $exampleDate);?>
 - //Not Working - output(2019-08-18T00:00:00+02:00)
 - <?php echo date("c", strtotime($exampleDate));?>
 - //Not Working - output(2019-09-23T19:36:01+02:00)
 - <?php echo (new DateTime($row['Time']))->format('c');?>
 - //Working Perfect - output(2019-08-18T00:00:00)
 - <?php echo date('Y-m-d\TH:i:s', strtotime($exampleDate));?> 

这会将日期时间从数据库转换为本地日期时间

str_replace(" ","T",substr_replace($string ,"", -3))

截至 2019 年,上述解决方案中的

None 使用 Google Chrome Version 78.0.3904.70 (Official Build) (64-bit)

对我有用

对我有用的是。

<input type="datetime-local" value="2017-06-13T13:00">

如您所见,格式为 2017-06-13T13:00Y-m-dTH:i

从 PHP 开始,您可以这样做。

<input type="datetime-local" value="<?php echo Date('Y-m-d\TH:i',time()) ?>">

希望这会节省一些人的时间。 :)

更好地使用时区功能

function getCurrentDateTime(){
    $date = new DateTime();
    $date->setTimezone(new DateTimeZone('GMT+6')); //Time Zone GMT +6
    $dt= $date->format('Y-m-d\TH:i:s');
    return $dt;
}

试一试:

<input type="datetime-local"  value="<?php $row['Time'] = preg_replace("/\s/",'T',$row['Time']); echo $row['Time']?>" class="date" name="start" REQUIRED>

对我来说最简单的工作方式是

<input  type="datetime-local"  value="<?= str_replace(' ', 'T', $date) ?>" name="date"  required />

试试看。我在 Bootstrap 4 中得到了。(Google 翻译:试试看,我在 Bootstrap 4 中得到了。)

<input class="form-control" type="datetime-local" name="fecha_e" id="fecha_e" value=" 
<?php echo date('Y-m-d\TH:i', strtotime($data['fecha_e_tarea'])); ?>" required>

对于 2021 年的每个人来说,工作格式是 Y-m-d\TH:i 所有其他旧格式都只是一个 hussel。 如果您正在使用碳库

 \Carbon\Carbon::parse($data->chosen_date,$timezone)->format('Y-m-d\TH:i')

之后你可以像这样传递给值 如果正在使用 ajax 或获取 api

document.getElementById('chosen_date').value = data.chosen_date

最简单的方法!以下是如何使用 php 为类型为“datetime-local”的输入字段设置值 在 type="datetime-local"

中使用 php 设置值
$date_now_time = date("Y-m-d H:i:s", strtotime('+5 hours'));
$from_for_val=  date("Y-m-d\TH:i:s", strtotime($date_now_time));

下面是 html 类型为“datetime-local”的表单输入字段及其显示如何在输入字段内设置值

   <input type="datetime-local" name="submit_from" value="<?=$from_for_val;?>" >