Jonah Katz Jonah Katz - 1 year ago 75
PHP Question

Add 'x' amount of hours to date

I currently have php get the current time/date like so:

$now = date("Y-m-d H:m:s");

What id like to do is have a new variable
being an amount of hours ranging anywhere from 24-800.

Any suggestions?

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download