What is the purpose of using the 'DateTime' data type in MySQL for storing timestamp values?

Using the 'DateTime' data type in MySQL for storing timestamp values allows for easier manipulation and comparison of date and time information within the database. It also ensures that the data is stored in a consistent format, which can help prevent errors and improve data integrity. Additionally, it provides built-in functions for date and time operations, making it simpler to work with timestamps in queries and applications.

// Create a table with a column of type DateTime for storing timestamp values
$sql = "CREATE TABLE example_table (
    id INT AUTO_INCREMENT PRIMARY KEY,
    timestamp_column DATETIME
)";
$result = mysqli_query($conn, $sql);

// Insert a timestamp value into the table
$timestamp = new DateTime();
$timestamp_str = $timestamp->format('Y-m-d H:i:s');
$sql = "INSERT INTO example_table (timestamp_column) VALUES ('$timestamp_str')";
$result = mysqli_query($conn, $sql);

// Retrieve timestamp values from the table
$sql = "SELECT * FROM example_table";
$result = mysqli_query($conn, $sql);
while($row = mysqli_fetch_assoc($result)) {
    $timestamp_value = $row['timestamp_column'];
    echo $timestamp_value . "\n";
}