php 如何为输入类型“datetime-local”设置值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40843407/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set a value for the input type 'datetime-local'?
提问by Jbadminton
I tried this:
我试过这个:
<input type="datetime-local" value="<?php echo $row['Time']; ?>" class="date" name="start" REQUIRED>
How can I set the value of this input field with the data from the database?
It doesn't work!!
I need to make it possible to edit too.
Or should I use another type of input?$row['Time']
is from the database!
如何使用数据库中的数据设置此输入字段的值?
它不起作用!!
我也需要让编辑成为可能。
或者我应该使用其他类型的输入?$row['Time']
来自数据库!
回答by Karol Gasienica
I don't know exacly what is in $row['Time']
but it should be as follows:
我不知道具体是什么,$row['Time']
但它应该如下:
Definition
定义
A valid date-time as defined in RFC 3339with 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
RFC 3339 中定义的有效日期时间,具有以下附加条件:
- 日期/时间语法中的文字字母 T 和 Z 必须始终大写
- date-fullyear 生产被定义为代表大于 0 的数字的四位或更多位数字
例子
- 1990-12-31T23:59:60Z
- 1996-12-19T16:39:57-08:00
Solution
解决方案
To create RFC 3339format in PHP you can use:
要在 PHP 中创建RFC 3339格式,您可以使用:
echo date('Y-m-d\TH:i:sP', $row['Time']);
or in another way:
或以另一种方式:
echo date("c", strtotime($row['Time']));
or if you prefer objective style:
或者如果您更喜欢客观风格:
echo (new DateTime($row['Time']))->format('c');
In your code
在你的代码中
So in your code it would look as follows:
因此,在您的代码中,它将如下所示:
<input type="datetime-local" value="<?php echo date('Y-m-d\TH:i:sP', $row['Time']); ?>" class="date" name="start" REQUIRED>
or
或者
<input type="datetime-local" value="<?php echo date("c", strtotime($row['Time'])); ?>" class="date" name="start" REQUIRED>
Manual
手动的
More informations can be found here
更多信息可以在这里找到
回答by Mohsin Shoukat
it's simple is that and working for me first convert your php value to this format
很简单,为我工作首先将您的 php 值转换为这种格式
<?php $datetime = new DateTime($timeinout[0]->time_in); ?>
then in value of html input element use this format
然后在 html input 元素的值中使用这种格式
<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" />
this will set your value to input element
这会将您的值设置为输入元素
回答by Ferhat KO?ER
You can use
您可以使用
date('Y-m-d\TH:i'); //Example result: '2017-01-01T01:01'
if use \T instead of T (not working)
如果使用 \T 而不是 T (不工作)
date('Y-m-dTH:i'); //Example result: '2017-01-01UTC01:01'
回答by ailia
The answer of Karol Gasienica is a great explanation but somehow did not work for me even in their replies
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
What worked for me is this
对我有用的是这个
$t = $row['Time'];
date('Y-m-d\TH:i:s', strtotime($t)); // This got it perfectly
However I still voted it up becauce of the explanation.
但是,由于解释,我仍然投票赞成。
回答by Rhalp Darren Cabrera
When submitting <form>
using <input type="datetime-local">
提交时<form>
使用<input type="datetime-local">
the value format you will get is look like this.
您将获得的值格式如下所示。
2019-09-06T00:21
2019-09-06T00:21
To set new value in your input type box.
在输入类型框中设置新值。
You must use:
您必须使用:
date('Y-m-d\TH:i', strtotime($exampleDate)) //2019-08-18T00:00
Solution Example:
解决方案示例:
$exampleDate = "2019-08-18 00:00:00";//sql timestamp
$exampleDate = strtotime($exampleDate);
$newDate = date('Y-m-d\TH:i', $exampleDate);
or
或者
$exampleDate = "2019-08-18 00:00:00";//sql timestamp
$newDate = date('Y-m-d\TH:i', strtotime($exampleDate));
If you dont use strtotime()
you will get an error of
如果你不使用strtotime()
你会得到一个错误
Notice: A non well formed numeric value encountered
注意:遇到格式不正确的数值
Tested测试:
- $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));?>
回答by Deepak Sharma
This will convert datetime from database to datetime-local
这会将日期时间从数据库转换为日期时间本地
str_replace(" ","T",substr_replace($string ,"", -3))
str_replace(" ","T",substr_replace($string ,"", -3))
回答by danish-khan-I
None of the above solutions worked for me as of 2019 using Google Chrome Version 78.0.3904.70 (Official Build) (64-bit)
截至 2019 年,使用 Google Chrome 的上述解决方案均不适用于我 Version 78.0.3904.70 (Official Build) (64-bit)
What worked for me is.
对我有用的是。
<input type="datetime-local" value="2017-06-13T13:00">
As you can see the format is 2017-06-13T13:00
or Y-m-dTH:i
.
如您所见,格式为2017-06-13T13:00
或Y-m-dTH:i
。
As of PHP you can do like.
从 PHP 开始,您可以这样做。
<input type="datetime-local" value="<?php echo Date('Y-m-d\TH:i',time()) ?>">
Hope this will save someone's time. :)
希望这会节省某人的时间。:)
回答by Rishi
$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
这是 Html 部分
<input type="datetime-local" name="trip_start_date" id="cal" value="<?php echo $string?>">