What is the best way to subtract 24 hours from a MySQL timestamp in PHP?
To subtract 24 hours from a MySQL timestamp in PHP, you can use the DateTime class to manipulate the timestamp. You can create a new DateTime object with the timestamp, then use the modify() method to subtract 24 hours from it. Finally, you can format the resulting timestamp back into the MySQL format.
$timestamp = "2022-01-01 12:00:00";
$datetime = new DateTime($timestamp);
$datetime->modify('-24 hours');
$new_timestamp = $datetime->format('Y-m-d H:i:s');
echo $new_timestamp;
Related Questions
- What are the potential drawbacks of using file_get_contents() function to read external text files in PHP?
- What are the benefits of using anonymous functions or closures in PHP for handling custom functions in WooCommerce?
- How can one optimize the performance of PHP scripts that involve database queries, especially when retrieving and displaying data from a MySQL database?