Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add 'x' number of hours to date

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?

like image 236
Jonah Katz Avatar asked Jul 08 '12 20:07

Jonah Katz


People also ask

How do you add hours to a date?

3. If you want to add hours, minutes and second to a date simultaneously, you can use this formula =A2+TIME(23,23,34), this means to add 23 hours, 23 minutes and 34 seconds to a date cell.

How do you add hours to current date and time?

To add hours in the current date-time, we use AddHours() method of DateTime class in C#. Syntax: DateTime DateTime. AddHours(double);

How to add hours to a date js?

To add hours to a Date in JavaScript:function addHours(numOfHours, date = new Date()) { date.

How to add hours to a date php?

Example 1: PHP Add Hours to DateTime$newDate = date('Y-m-d H:i:s', strtotime($date. ' + 3 hours')); echo $newDate; ?>


1 Answers

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.

like image 181
fdomig Avatar answered Oct 12 '22 20:10

fdomig