php - Add 'x' number of hours to date
Get the solution ↓↓↓I currently have php returning the current date/time like so:
$now = date("Y-m-d H:m:s");
What I'd like to do is have a new variable$new_time
equal$now + $hours
, where$hours
is a number of hours ranging from 24 to 800.
Any suggestions?
Answer
Solution:
You may use something like the function to add something to the current timestamp.
$new_time = date("Y-m-d H:i:s", strtotime('+5 hours'))
.
If you need variables in the function, you must use double quotes then likestrtotime("+{$hours} hours")
, however better you usestrtotime(sprintf("+%d hours", $hours))
then.
Answer
Solution:
An other solution (object-oriented) is to use DateTime::add
Example:
<?php
$now = new DateTime(); //now
echo $now->format('Y-m-d H:i:s'); // 2021-09-11 01:01:55
$hours = 36; // hours amount (integer) you want to add
$modified = (clone $now)->add(new DateInterval("PT{$hours}H")); // use clone to avoid modification of $now object
echo "\n". $modified->format('Y-m-d H:i:s'); // 2021-09-12 13:01:55
Answer
Solution:
You can use strtotime() to achieve this:
$new_time = date("Y-m-d H:i:s", strtotime('+3 hours', $now)); // $now + 3 hours
Answer
Solution:
Correct
You can use strtotime() to achieve this:
$new_time = date("Y-m-d H:i:s", strtotime('+3 hours', strtotime($now))); // $now + 3 hours
Answer
Solution:
You can also use the unix style time to calculate:
$newtime = time() + ($hours * 60 * 60); // hours; 60 mins; 60secs
echo 'Now: '. date('Y-m-d') ."\n";
echo 'Next Week: '. date('Y-m-d', $newtime) ."\n";
Answer
Solution:
Um... your minutes should be corrected... 'i' is for minutes. Not months. :) (I had the same problem for something too.
$now = date("Y-m-d H:i:s");
$new_time = date("Y-m-d H:i:s", strtotime('+3 hours', $now)); // $now + 3 hours
Answer
Solution:
I use this , its working cool.
//set timezone
date_default_timezone_set('GMT');
//set an date and time to work with
$start = '2014-06-01 14:00:00';
//display the converted time
echo date('Y-m-d H:i',strtotime('+1 hour +20 minutes',strtotime($start)));
Answer
Solution:
You can try lib Ouzo goodies, and do this in fluent way:
echo Clock::now()->plusHours($hours)->format("Y-m-d H:m:s");
API's allow multiple operations.
Answer
Solution:
For a given DateTime, you can add days, hours, minutes, etc. Here's some examples:
$now = new \DateTime();
$now->add(new DateInterval('PT24H')); // adds 24 hours
$now->add(new DateInterval('P2D')); // adds 2 days
PHP: DateTime::add - Manual https://www.php.net/manual/fr/datetime.add.php
Answer
Solution:
$date_to_be-added="2018-04-11 10:04:46";
$added_date=date("Y-m-d H:i:s",strtotime('+24 hours', strtotime($date_to_be)));
A combination of date() and strtotime() functions will do the trick.
Answer
Solution:
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 thosestrtotime
anddate
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 addingx
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.
Answer
Solution:
for add 2 hours to "now"
$date = new DateTime('now +2 hours');
or
$date = date("Y-m-d H:i:s", strtotime('+2 hours', $now)); // as above in example
or
$now = new DateTime();
$now->add(new DateInterval('PT2H')); // as above in example
Answer
Solution:
$now = date("Y-m-d H:i:s");
date("Y-m-d H:i:s", strtotime("+1 hours $now"));
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: xmlhttprequest error flutter
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.