Subtract hours from a time and date using PHP.

In this tutorial, we are going to show you how to subtract hours from a date and time using PHP.

This can be useful if you need to work out an exact date and time in the recent past.

For example, “What time was it six hours ago?” or “What time was it twelve hours ago?”

Subtracting hours using PHP’s time function.

You can subtract hours using PHP’s time function and some basic arithmetic:

//Get the current timestamp.
$currentTime = time();

//The number of hours that you want
//to subtract from the date and time.
$hoursToSubtract = 12;

//Convert those hours into seconds so
//that we can subtract them from our timestamp.
$timeToSubtract = ($hoursToSubtract * 60 * 60);

//Subtract the hours from our Unix timestamp.
$timeInPast = $currentTime - $timeToSubtract;

//Print it out in a human-readable format.
echo date("d/m/y H:i", $timeInPast) . ' was ' . $hoursToSubtract . ' hours ago.';

An explanation of the PHP code above:

  1. We got the current Unix timestamp using PHP’s time function.
  2. We defined a variable called $hoursToSubtract. This variable contains the number of hours that we want to subtract from our date and time.
  3. After that, we converted our hours into seconds by multiplying them by 3600. This works because there are exactly 3600 seconds in one hour (60 x 60 = 3600). In the case above, 12 x 3600 = 43200.
  4. Finally, we subtracted those seconds from our timestamp and then printed out the result in a human friendly date format.

When we ran the example above at 14:52 p.m. on January 9th, 2020, the output was:

09/01/20 02:52 was 12 hours ago.

As you can see, this is correct, as 12 hours before 14:52PM was 2:52AM.

Using PHP’s DateTime object to subtract hours from a date.

You can also use PHP’s DateTime object to subtract hours from a given date and time.

Take the following example:

//Get the current timestamp.
$current = new DateTime();

//The number of hours to subtract.
$hoursToSubtract = 12;

//Subtract the hours by using the DateTime::sub method.
$current->sub(new DateInterval("PT{$hoursToSubtract}H"));

//Format the new time.
$newTime = $current->format('Y-m-d H:i');
echo $newTime;

In the example above, we used the DateTime::sub method to subtract a period of 12 hours from the current timestamp.

Essentially, we passed in a DateInterval object with PT12H as the $interval_spec parameter.

PT12H basically means “a period of time of 12 hours.” If you want to subtract 24 instead of 12, you can use PT24H.

If you want to subtract hours from a given date, you can do the following:

//Datetime object for a given date.
$dT = new DateTime("2020-01-09 01:00:00");

//Lets subtract 4 hours.
$hoursToSubtract = 4;

//Subtract the hours using DateTime::sub and DateInterval.
$dT->sub(new DateInterval("PT{$hoursToSubtract}H"));

//Format and print it out.
$newTime = $dT->format('Y-m-d H:i');
echo $newTime;

If you run the DateTime code above, you will see that the output is “2020-01-08 21:00”.

This is correct, as 2020-01-08 21:00 occurred exactly four hours before 2020-01-09 01:00.

Related tutorials.