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 are some best practices for storing and managing bot data in a MySQL database when developing a bot detection script in PHP?
- What functions can be used in PHP to randomly assign logged-in users to matches in a league system?
- What are the best practices for filtering out "." and ".." entries when scanning a directory with PHP?