How to Compare MySQL|date Types

Asked 2 years ago, Updated 2 years ago, 72 views

From the form submission of html, we try to compare the value of type = "date" with the column of type date in SQL, but we cannot extract it.
The source code is as follows:
Is there something wrong with the SQL, PHP, and html form?

<form action="result.php" method="post">
  <input type="date" name="search_date">
  <input type="submit">
</form>

// event_date column is of type date
$search_date=$_POST["search_date"];
$sql="select * from sample where event_date=?";
$stmt = $dbh->prepare($sql);
// DB handler ready
$stmt->execute($stmt,$search_date);

By the way, is it possible to use the value of type="date" as an extraction condition for the date type column?

php html mysql sql

2022-09-30 20:52

1 Answers

The value sent from the form is in ISO 8601 format, so

2015-08-01T02:03:04+09:00

The date and time are separated by T and the time zone designation is appended to the end as shown in .

MySQL, on the other hand, is recognizable in its format:

  • 15-08-01
  • 2015-08-01
  • 2015-08-0102:03:04

There are 6 different types of symbol differences and unsymbolized versions, so the SQL being questioned is not recognized as a date, and you may be comparing event_date to a non-ISO8601 string before comparing it to the parameters.

In order to recognize the date as a date, PHP should convert it like date('Y-m-d', strtotime($_POST["search_date").


2022-09-30 20:52

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.