Add hours to existing date php

I like those built-in php date expressions like +1 hour, but for some reason, they fall out of my head all of the time. Besides, none of the IDEs I'm aware of suggest auto-completion facility for that kind of stuff. And, finally, although juggling with those strtotime and date functions is no rocket science, I have to google their usage each time I need them.

That's why I like the solution that eliminates (at least mitigates) those issues. Here's how adding x hours to a date can look like:

(new Future(
    new DateTimeFromISO8601String('2014-11-21T06:04:31.321987+00:00'),
    new NHours($x)
))
    ->value();

As a nice bonus, you don't have to worry about formatting the resulting value, it's already is ISO8601 format.

This example uses meringue library, you can check out more examples here.

| Posted on May 9, 2021 |

The following is really easy way to add hours in current date and time using PHP. Using the date function to set the format of the date to be returned then using strtotime.

The following source code will add 2 hours to current date and time.

The following source code will add 2 hours to date and time.


And if you like this tutorials please share it with your friends via Email or Social Media.

Post Navigation ← Previous PostNext Post →

This is a tutorial on how to add hours to a date and time using PHP.

In this guide, we will provide examples using both the regular date and time functions, as well as PHP’s DateTime object.

Adding hours onto a date and time using PHP’s date and time functions.

Take a look at the following example, which is intentionally verbose:

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

//The amount of hours that you want to add.
$hoursToAdd = 2;

//Convert the hours into seconds.
$secondsToAdd = $hoursToAdd * (60 * 60);

//Add the seconds onto the current Unix timestamp.
$newTime = $currentTime + $secondsToAdd;

//Print it out in a format that suits you.
echo date("d/m/y H:i", $newTime), '
'; //Or try this. echo "$hoursToAdd hour(s) added onto " . date("d/m/y H:i", $currentTime) . " becomes " . date("d/m/y H:i", $newTime);

In the PHP code above, we:

  • Got the current Unix timestamp using PHP’s time function.
  • Defined a variable called $hoursToAdd, which contains the number of hours that we want to add onto our date and time. In this case, we will be adding 2 hours to the current time. If we wanted to add 12 hours to the current time, then we would simply change this variable to 12.
  • We then converted our hours into seconds by multiplying the number of hours by 3600 (60 x 60 = 3600). This works because there are 3600 seconds in one hour. In the case above, 2 is multiplied by 3600, which gives us 7200. This is correct, as there are 7200 seconds in two hours.
  • We then added these seconds onto the current timestamp.
  • Finally, we printed out the new date and time using PHP’s date function.

When I ran the code above, I received the following result:

2 hour(s) added onto 03/01/20 14:10 becomes 03/01/20 16:10

Perfect!

Adding hours onto a date and time using PHP’s DateTime object.

If you want to use the DateTime object for this operation, then you can use the following example:

//Get the current date and time.
$current = new DateTime();

//The number of hours to add.
$hoursToAdd = 2;

//Add the hours by using the DateTime::add method in
//conjunction with the DateInterval object.
$current->add(new DateInterval("PT{$hoursToAdd}H"));

//Format the new time into a more human-friendly format
//and print it out.
$newTime = $current->format('Y-m-d H:i');
echo $newTime;

As you can see, you do not need to do any multiplication sums while using the DateTime object, as it comes equipped with a handy method called DateTime::add.

In the code above, we simply passed a DateInterval object into this method. Our DateInterval object had PT2H set as the $interval_spec parameter, which basically translates into “a period of two hours.”

Other examples of what you could pass into the constructor of the DateInterval object:

  • PT1H: A period of one hour.
  • PT6H: A period of six hours.
  • PT24H: A period of 24 hours.

The most important thing in this case is that we use the letter “H” as the Period Designator.

Related tutorials:

  • Subtract hours from a date using PHP.
  • Add years onto a date using PHP.
  • Add days onto a date using PHP.

How do you add 2 hours to a timestamp?

To add 2 hours to the Date Object first, we get the current time by using the Date. getTime() method and then add 2 hour's milliseconds value (2 * 60 * 60 * 1000) to it and pass the added value to the Date Object.

How do you add hours on Strtotime?

You can use DateTime::modify to add time, but I would just do time()+10800 . Show activity on this post. $time = new DateTime("+ 3 hour"); $timestamp = $time->format('Y-M-d h:i:s a');

How can I add hours and minutes in PHP?

php function CalculateTime($times) { $i = 0; foreach ($times as $time) { sscanf($time, '%d:%d', $hour, $min); $i += $hour * 60 + $min; } if($h = floor($i / 60)) { $i %= 60; } return sprintf('%02d:%02d', $h, $i); } $date[] = '02:32'; $date[] = '01:29'; echo CalculateTime($date); ?>

How can increase day in date in PHP?

You can use strtotime. $your_date = strtotime("1 day", strtotime("2016-08-24")); $new_date = date("Y-m-d", $your_date);