How can PHP code be adjusted to ensure that dates are stored in the correct format (YYYY-MM-DD) in a MySQL database?
When storing dates in a MySQL database using PHP, it's important to ensure that the dates are in the correct format (YYYY-MM-DD) to avoid any issues with date manipulation or retrieval. To achieve this, you can use PHP's date() function to format the date before inserting it into the database.
// Assuming $date contains the date to be stored in the database
$formatted_date = date('Y-m-d', strtotime($date));
// Inserting the formatted date into the database
$query = "INSERT INTO table_name (date_column) VALUES ('$formatted_date')";
// Execute the query using your preferred method (mysqli_query, PDO, etc.)
Keywords
Related Questions
- Are there any common pitfalls or mistakes to avoid when handling binary files in PHP, especially on different server environments?
- What are common issues when replacing special characters like umlauts in PHP filenames?
- What is the correct syntax for passing values to included PHP files using a switch statement?