PHP 时间戳到 HTML 5 输入 type=datetime 元素

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12695082/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 04:03:13  来源:igfitidea点击:

PHP timestamp to HTML 5 input type=datetime element

phphtmlinputtimestamp

提问by thatidiotguy

I have a unix timestamp (int) in PHP. I want to display this value in a nice manner in the HTML5 datetime input element. I would like to be able to have users see this value in a nice presentable manner, as well as edit it. Is there any nice way of doing this, or am I fooling myself and I am going to have to fuss around with lots of string manipulation?

我在 PHP 中有一个 unix 时间戳 (int)。我想在 HTML5 日期时间输入元素中以一种很好的方式显示这个值。我希望能够让用户以一种很好的呈现方式看到这个值,并对其进行编辑。有什么好的方法可以做到这一点,还是我在自欺欺人,我将不得不为大量的字符串操作而大惊小怪?

回答by Baba

HTML5 Input timeis something like this : 1985-04-12T23:20:50.52

HTML5 Input time是这样的:1985-04-12T23:20:50.52

You can do that in PHP like this :

你可以在 PHP 中这样做:

echo date("Y-m-d\TH:i:s");

Output

输出

2012-10-02T19:12:49

HTML with Timestamp

带时间戳的 HTML

<input type="datetime"  value="<?php echo date("Y-m-d\TH:i:s",$timestamp); ?>"/>

回答by cameron

The other examples are both the old way to produce a valid timeStamp format for the TIME element -- the new hotness in php:

其他示例都是为 TIME 元素生成有效时间戳格式的旧方法——php 中的新热点:

<time><?php echo date(DATE_W3C); ?></time>

That will generate a format like:

这将生成如下格式:

"2013-05-19T06:40:08+00:00"

“2013-05-19T06:40:08+00:00”

回答by Jason McCreary

According to the HTML5 Specification for input type="datetime", it should be as simple as setting the valueattribute.

根据输入 type="datetime"HTML5 规范,它应该像设置value属性一样简单。

value="<?php echo date('c', $timestamp); ?>"

See PHP's date() functionfor additional formatters.

有关其他格式化程序,请参阅PHP 的 date() 函数

回答by Zaheer

Datetimedoesn't work for me on chrome, datetime-localallows me to choose date and time from popup.

Datetime在 chrome 上对我不起作用,datetime-local允许我从弹出窗口中选择日期和时间。

example

例子

<input type="datetime-local" name="xyz" value="<?php echo date("Y-m-d\TH:i:s",time()); ?>"/>

time() functions is not relevant ,I am using it to make it easy for user to choose from current.. time after or before.

time() 函数不相关,我使用它是为了让用户可以轻松地从当前时间中选择之后或之前。