我正在尝试从日期输入表单中检索日期,但无法正常工作.我现在使用的代码只返回一个错误和这个日期:1970-01-01.这是不正确的,我想知道如何解决这个问题.这将在稍后用于查询数据库表.我希望日期基本上只是一个具有以下格式的字符串:yyyy-mm-dd"
I'm trying to retrieve a date from a date input form and I just can't get it to work properly. The code I'm using now only returns an error and this date: 1970-01-01. That is not correct and I would like to know how I can approach this. This will be used later to query a database table. I want the date to be basically just a string with this format: "yyyy-mm-dd"
代码:
HTML
<form name="DateFilter" method="POST">
From:
<input type="date" name="dateFrom" value="<?php echo date('Y-m-d'); ?>" />
<br/>
To:
<input type="date" name="dateTo" value="<?php echo date('Y-m-d'); ?>" />
</form>
PHP
$new_date = date('Y-m-d', strtotime($_POST['dateFrom']));
echo $new_date;
提前致谢,
~现实
~~~ 编辑~~~
为任何想知道它是如何完成的人提供的固定解决方案:
HTML
<form name="Filter" method="POST">
From:
<input type="date" name="dateFrom" value="<?php echo date('Y-m-d'); ?>" />
<br/>
To:
<input type="date" name="dateTo" value="<?php echo date('Y-m-d'); ?>" />
<input type="submit" name="submit" value="Login"/>
</form>
PHP
$new_date = date('Y-m-d', strtotime($_POST['dateFrom']));
echo $new_date;
特别感谢每一个回答.
验证输入.
$time = strtotime($_POST['dateFrom']);
if ($time) {
$new_date = date('Y-m-d', $time);
echo $new_date;
} else {
echo 'Invalid Date: ' . $_POST['dateFrom'];
// fix it.
}
这篇关于从 PHP 中的输入表单获取日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!