php 输入类型日期 html5 并从数据库接收数据

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

input type date html5 and receiving data from databse

phphtmldateinput

提问by Stanislas Piotrowski

I'm working on Chromelocally with WAMP, PHPversion 5. I use an input type date, which displays a calendar. In fact I get some variables within the data editing page of my users. The concern I have is that all the data are collected. However for date, instead of displaying the record in the database (confirmed by var_dump) he displays a placeholder Day / Month / Year

我工作的Chrome地方规定WAMPPHP版本5.我使用的输入型日期,这显示日历。事实上,我在用户的数据编辑页面中得到了一些变量。我担心的是所有数据都被收集了。但是对于日期,不是在数据库中显示记录(由 确认var_dump),而是显示一个占位符Day / Month / Year

which does not interest me, I want this placeholder if and only if my field is empty.

我不感兴趣,当且仅当我的字段为空时,我才想要这个占位符。

Here is the syntax of the field:

这是该字段的语法:

<input name="datedepart" type="date"  value="<?php echo date('d/m/Y',strtotime($data["congestart"])) ?>"/>

Is there a way for that?

有办法吗?

Receive dear All my utmost respect.

收到亲爱的,我最崇高的敬意。

回答by fdreger

Yes, it will probably be enough to change the date format to ISO, like 2012-08-22:

是的,将日期格式更改为 ISO 可能就足够了,例如 2012-08-22:

<input name="datedepart" type="date"
value="<?php echo date('Y-m-d',strtotime($data["congestart"])) ?>"/>

If your server expects some other format, you will have to convert it using Javascript (ISO is the standard used in HTML5 specification, so the value of the input field will always be ISO, no matter how Chrome displays it).

如果您的服务器需要其他格式,则必须使用 Javascript 进行转换(ISO 是 HTML5 规范中使用的标准,因此输入字段的值将始终为 ISO,无论 Chrome 如何显示它)。

update, clarification:

更新,澄清:

Date field can only contain a valid date. When you try to set it it to some random garbage, then the value becomes empty. When the value is empty - Chrome displays the placeholder. The only valid format for dates in HTML5 is ISO: 2012-03-04. Just try and see:

日期字段只能包含有效日期。当您尝试将其设置为一些随机垃圾时,该值将变为空。当值为空时 - Chrome 显示占位符。HTML5 中唯一有效的日期格式是 ISO:2012-03-04。试试看:

<input value='2004-02-12' type='date'>

回答by devWaleed

Use

placeholder="<?php echo date('d/m/Y',strtotime($data["congestart"])) ?>"

instead of putting it in value.

而不是将其转化为价值。

If this doesn't solve the answer, Then explain a little more about what you want to do?

如果这不能解决答案,那么再解释一下你想做什么?

回答by Edmund Chang

This works very well from fdreger:

这在fdreger 中非常有效:

<input name="datedepart" type="date" value="<?php echo date('Y-m-   d',strtotime($data["congestart"])) ?>"/>

I needed the same thing.

我需要同样的东西。

回答by Jaffar Hussain

I know its old thread but i think this can be helpful for someone

我知道它的旧线程,但我认为这对某人有帮助

Try to use min and max then this will pick your provided value.

尝试使用 min 和 max 然后这将选择您提供的值。

Like

喜欢

<input type="date" min="2014-09-08" max="2018-09-08" value="2015-02-24" >

回答by Abhijit Jagtap

Its work for me

它为我工作

<input class="form-control calendar" name="datedepart" required="" type="date" id="datedepart" aria-required="true" value="<?php echo date('Y-m-d',strtotime($data["congestart"])) ?>">

回答by Arek Stopa

what worked for me was to remove quotes around the value returned from php, so in the case of your code it would try this:

对我有用的是删除从 php 返回的值周围的引号,所以在你的代码的情况下,它会尝试这个:

<input name="datedepart" type="date"  value=<?php echo date('d/m/Y',strtotime($data["congestart"])) ?>/>

回答by Salman Aziz

This one works for me

这个对我有用

    <input name="datedepart" type="date" value=<?php echo strftime('%Y-%m-%d', strtotime($data['congestart'])); ?>"/>