What are some best practices for storing and retrieving dates from a MySQL database in PHP?

When storing and retrieving dates from a MySQL database in PHP, it is important to use the correct date format to ensure consistency and accuracy. One common practice is to store dates in the database using the MySQL DATE or DATETIME data types. When retrieving dates in PHP, you can use the date() function to format the date according to your needs.

// Storing a date in the database
$date = date('Y-m-d H:i:s'); // Current date and time
$query = "INSERT INTO table_name (date_column) VALUES ('$date')";
// Execute the query

// Retrieving a date from the database
$query = "SELECT date_column FROM table_name WHERE id = 1";
$result = mysqli_query($connection, $query);
$row = mysqli_fetch_assoc($result);
$date = date('Y-m-d H:i:s', strtotime($row['date_column']));
echo $date;