Add hours to existing date php
I like those built-in php date expressions like Show That's why I like the solution that eliminates (at least mitigates) those issues. Here's how adding
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. 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), ' In the PHP code above, we:
When I ran the code above, I received the following result:
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:
The most important thing in this case is that we use the letter “H” as the Period Designator. Related tutorials:
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);
|