Add 'x' amount of hours to date
3Answer
You may use something like the strtotime()
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 like strtotime("+{$hours} hours")
, however better you use strtotime(sprintf("+%d hours", $hours))
then.
- answered 8 years ago
- G John
An other solution (object-oriented) is to use DateTime::add
Exemple :
$now = new DateTime(); //current date/time
$now->add(new DateInterval("PT{$hours}H"));
$new_time = $now->format('Y-m-d H:i:s');
PHP documentation : http://fr2.php.net/manual/en/datetime.add.php
- answered 8 years ago
- G John
You can use strtotime() to achieve this:
$new_time = date("Y-m-d H:i:s", strtotime('+3 hours', $now)); // $now + 3 hours
- answered 8 years ago
- Gul Hafiz
Your Answer