从 PHP 中的输入表单获取日期

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

Get date from input form within PHP

phphtmldatedatepicker

提问by Realitiez

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"

我正在尝试从日期输入表单中检索日期,但我无法使其正常工作。我现在使用的代码只返回一个错误和这个日期:1970-01-01。这是不正确的,我想知道如何解决这个问题。这将在稍后用于查询数据库表。我希望日期基本上只是一个具有以下格式的字符串:“yyyy-mm-dd”

Code:

代码:

HTML

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

PHP

$new_date = date('Y-m-d', strtotime($_POST['dateFrom']));
echo $new_date;

Thanks in advance,

提前致谢,

~ Realitiez

~现实

~~~ EDIT ~~~

~~~编辑~~~

Fixed Solution for anyone wondering how it's done:

任何想知道它是如何完成的人的固定解决方案:

HTML

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

PHP

$new_date = date('Y-m-d', strtotime($_POST['dateFrom']));
echo $new_date;

Special Thanks to every answer.

特别感谢每一个回答。

回答by Misunderstood

Validate the INPUT.

验证输入。

$time = strtotime($_POST['dateFrom']);
if ($time) {
  $new_date = date('Y-m-d', $time);
  echo $new_date;
} else {
   echo 'Invalid Date: ' . $_POST['dateFrom'];
  // fix it.
}

回答by Amit Ghosh Anto

    <?php
if (isset($_POST['birthdate'])) {
    $timestamp = strtotime($_POST['birthdate']); 
    $date=date('d',$timestamp);
    $month=date('m',$timestamp);
    $year=date('Y',$timestamp);
}
?>