What are the benefits of using DATETIME data type instead of INT for storing dates in MySQL tables in PHP?

Using the DATETIME data type instead of INT for storing dates in MySQL tables in PHP allows for easier manipulation and comparison of dates within the database. DATETIME data type also provides built-in functions for date calculations and formatting, making it more convenient to work with dates in queries and scripts. Additionally, DATETIME data type ensures data integrity by enforcing date format constraints.

// Create a table with a DATETIME column to store dates
$sql = "CREATE TABLE my_table (
    id INT AUTO_INCREMENT PRIMARY KEY,
    event_date DATETIME
)";
mysqli_query($conn, $sql);

// Insert a date into the table
$date = date("Y-m-d H:i:s");
$sql = "INSERT INTO my_table (event_date) VALUES ('$date')";
mysqli_query($conn, $sql);

// Retrieve and display dates from the table
$sql = "SELECT event_date FROM my_table";
$result = mysqli_query($conn, $sql);
while($row = mysqli_fetch_assoc($result)) {
    echo $row['event_date'] . "<br>";
}