How to subtract hours from the time in PHP
In this tutorial, we will learn How to subtract hours from the time in PHP. I will explain an easy way to create PHP subtract hours from the current time. You understand how to subtract hours from the time in PHP. So, let’s discuss how to subtract hours from the current time in PHP.
Also read: How to subtract minutes from time in PHP
Here, I will provide two examples.
- PHP Subtract hours to time.
- PHP Subtract hours to Current time.
I will give you a simple example of how to subtract hours from the time in PHP. Here, create a simple example of subtracting 2 hours from the current time in PHP.
Also read: How to subtract seconds from time in PHP
PHP Subtract hours to time
Example
<?php
$custom_time = "10:00:10";
$new_time = date('H:i:s', strtotime($custom_time. ' -2 hours'));
echo $new_time;
?>
Output
08:00:10
PHP Subtract hours to Current time
Example
<?php
// current time: 10:40:42
$new_time = date('H:i:s', strtotime(' -2 hours'));
echo $new_time;
?>
Output
8:40:42