简体   繁体   English

如何从HTML5输入类型=“日期”中获取日期值

[英]How to get the Date value from HTML5 input type=“date”

This is the user interface where user chooses the fromdate and todate from the form. 这是用户界面,用户可以从中选择表单的fromdatetodate日期。

 <html> <form name="frmSearch" method="post" action="searchdate2.php"> <label> From date </label> <input type="date" name="fromdate" /> <label> To date</label> <input type="date" name="todate" /> <input type="submit" name="go" value="Search"> </form> 

Then in searchdate2.php, I am trying to display the result with matching date range from mysql 然后在searchdate2.php中,我试图显示与mysql匹配的日期范围的结果

include('connect-db.php');

   $result = mysqli_query("SELECT * FROM treatmentdetail 
   WHERE nextdate BETWEEN '" . $_POST['fromdate'] . "' AND  '" . 
   $_POST['todate'] . "'");


 while($row = mysqli_fetch_array( $result ,MYSQL_ASSOC)) {

  echo "<tr>";
  echo '<td width="100px">' . $row['id'] . '</td>';
  echo '<td width="200px">' . $row['patientid'] . '</td>';
  echo '<td width="200px">' . $row['treatmentid'] . '</td>';

   }

  echo "</tr>";
  echo "</table>";
   ?>

I'm trying to figure out how to get the Date value using HTML5 input type="date", 我试图弄清楚如何使用HTML5输入type =“ date”获取Date值,

Can I do it in PHP or JQuery? 我可以用PHP或JQuery做到吗?

Can I do it in PHP or JQuery? 我可以用PHP或JQuery做到吗?

In JavaScript, on the browser, you can get it from the valueAsDate property : 在JavaScript的浏览器中,您可以从valueAsDate属性获取它:

// Without jQuery:
console.log(document.querySelector("input[name=fromdate]).valueAsDate);
// With jQuery:
console.log($("input[name=fromdate])[0].prop("valueAsDate"));

To access it from PHP, you'll need to submit the form and get the string value from $_GET or $_POST (as appropriate) and parse it. 要从PHP访问它,您需要提交表单并从$_GET$_POST (根据需要)获取字符串值并进行解析。

BETWEEN SQL operator works with numeric values... You are giving it strings. BETWEEN SQL运算符之间使用数字值...您正在为其提供字符串。

I suggest you use the > and < operators, which works with strings: 我建议您使用><运算符,它们适用于字符串:

$result = mysqli_query("SELECT * FROM treatmentdetail WHERE nextdate > '" . $_POST['fromdate'] . "' AND nextdate <  '" . $_POST['todate'] . "'");



EDIT 编辑

From your last comment ( «if i have 2017-7-1 in the db...» ), I think I caught your actual problem. 从您的最后一条评论( «如果我在数据库中有2017-7-1 ...» ),我想我发现了您的实际问题。
The HTML <input type="date"> returns the leading zeros. HTML <input type="date">返回前导零。

And the leading zeros are missing on month and date in your database. 并且数据库中月份和日期的前导零均缺失
You are a human, you don't need the leading zero to understand that "2017-7-1" is the same date as "2017-07-01". 您是人类,不需要前导零即可了解“ 2017-7-1”与“ 2017-07-01”是同一日期。

But a machine doesn't agree on that. 但是机器对此并不认同。
To it, those aren't dates... Those are string. 到目前为止,这些不是日期,而是字符串。 And the first is a greater value because 7 is greater than 0 at character position 5. 第一个是一个较大的值,因为在字符位置5处7大于0

"abcde" is greater than "aaabc" “ abcde”大于“ aaabc”
"12345" is greater than "11123". “ 12345”大于“ 11123”。

See here: 看这里:

 date_db = "2017-7-1"; date_post="2017-07-01"; greater = (date_db > date_post); equal = (date_db == date_post); smaller = (date_db < date_post); console.log(greater); console.log(equal); console.log(smaller); 

I would fix my database data. 我会修复我的数据库数据。
You just need to clean it once to avoid some complicated scripts that you would need to walk around some malformed data. 您只需要清理一次即可避免一些复杂的脚本,这些脚本需要处理一些格式错误的数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM