How can DATE_FORMAT be used in the WHERE clause of a SQL query in PHP?
When using DATE_FORMAT in the WHERE clause of a SQL query in PHP, you need to ensure that the date format matches the format stored in the database. You can use the DATE_FORMAT function to format the date in the query to match the database's date format. Example PHP code snippet:
$date = "2022-12-31";
$formatted_date = date("Y-m-d", strtotime($date));
$sql = "SELECT * FROM table_name WHERE DATE_FORMAT(date_column, '%Y-%m-%d') = '$formatted_date'";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
while ($row = mysqli_fetch_assoc($result)) {
// Process the results
}
} else {
echo "No results found.";
}
Keywords
Related Questions
- What is the best practice for calculating the total price of items in a shopping cart using PHP?
- How can you combine the mysql_query and mysql_fetch_object functions in PHP to achieve a desired result?
- What are some common pitfalls to avoid when working with PHP and MySQL together in a web application?