How can the PHP mktime() function be utilized to convert dates from a MySQL database to timestamps for accurate comparisons?

When retrieving dates from a MySQL database, they are typically in the format "YYYY-MM-DD HH:MM:SS". To accurately compare these dates or perform date calculations in PHP, it is necessary to convert them to timestamps. The PHP mktime() function can be utilized to convert these dates to timestamps, which are integer values representing the number of seconds since the Unix Epoch (January 1, 1970).

// Retrieve date from MySQL database
$dateFromDatabase = "2022-01-15 14:30:00";

// Convert date to timestamp using mktime()
$timestamp = mktime(
    substr($dateFromDatabase, 11, 2), // Hour
    substr($dateFromDatabase, 14, 2), // Minute
    substr($dateFromDatabase, 17, 2), // Second
    substr($dateFromDatabase, 5, 2),  // Month
    substr($dateFromDatabase, 8, 2),  // Day
    substr($dateFromDatabase, 0, 4)   // Year
);

echo $timestamp; // Output: 1642252200