92

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?

ashleedawg
  • 20,365
  • 9
  • 72
  • 105
Jonah Katz
  • 5,230
  • 16
  • 67
  • 90
  • 1
    strtotime() http://php.net/manual/en/function.strtotime.php –  Jul 08 '12 at 20:14
  • 4
    I doubt if it is what you want. I want you to notice that "m" in "H:m:s" represents month on digit. I believe you wanted to write "H:i:s". Please crosscheck – Paullo Aug 14 '14 at 14:59

13 Answers13

141

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.

Coderji
  • 7,655
  • 5
  • 37
  • 51
fdomig
  • 4,417
  • 3
  • 26
  • 39
  • That works, but if i replace the '5' with a variable, it doesnt work properly. `strtotime('+$hours hours')` – Jonah Katz Jul 08 '12 at 20:30
  • 1
    Don't you mean $new_time = date("Y-m-d H:i:s", strtotime('+5 hours')? m is month, i is minutes... – richard Aug 14 '13 at 07:54
  • For me, the use of `{$hours}` `strtotime("+{$hours} hours")` did not work. I replaced `{$hours}` with `'.$hours.'`. So instead of an {} I used ``. Hope this helps someone in need – Prince Michael Sep 08 '22 at 11:40
53

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

Run script


FAjir
  • 4,384
  • 2
  • 26
  • 30
  • 3
    Small tyop here, $date->format should probably be $now->format – Patrick Jan 11 '15 at 22:18
  • While I appreciate the link to PHP Documentation, it would be nice to describe what the parameters in your example do. You linked to the `DateTime::add` function, which takes the parameter of `DateInterval` whose doc then says to refer to the formats supported by the `DateTime` constructor which then points to "Date and Time Formats" which then links to "Time Formats", "Date Formats", "Compound Formats" and "Relative Formats". None of these seem to mention `P`, though I could very well have missed it in all those pages. I get that `T` means time, `H` hours, but `P`?.... – s.co.tt Apr 22 '21 at 23:55
  • @s.co.tt https://www.php.net/manual/en/dateinterval.construct.php The format starts with the letter P, for period. Each duration period is represented by an integer value followed by a period designator. If the duration contains time elements, that portion of the specification is preceded by the letter T. – FAjir Apr 24 '21 at 11:28
  • Thanks! I did figure it out, and please note I was irritated with the PHP docs, not the poster. But I'm glad your answer is here for posterity! – s.co.tt Jun 06 '21 at 16:44
26

You can use strtotime() to achieve this:

$new_time = date("Y-m-d H:i:s", strtotime('+3 hours', $now)); // $now + 3 hours
Zar
  • 6,786
  • 8
  • 54
  • 76
15

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
Rodrigo Prazim
  • 788
  • 7
  • 14
7

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";
Ankit
  • 3,878
  • 5
  • 35
  • 51
7

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)));
6

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
vr_driver
  • 1,867
  • 28
  • 39
5

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
toxab
  • 76
  • 1
  • 5
3

$to = date('Y-m-d H:i:s'); //"2022-01-09 12:55:46"

$from = date("Y-m-d H:i:s", strtotime("$to -3 hours")); // 2022-01-09 09:55:46

  • 1
    While this code snippet may solve the problem, it doesn't explain why or how it answers the question. Please [include an explanation for your code](//meta.stackexchange.com/q/114762/269535), as that really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. – Luca Kiebel Jan 09 '22 at 17:09
2

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.

Piotr Olaszewski
  • 6,017
  • 5
  • 38
  • 65
2

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

faye.babacar78
  • 774
  • 7
  • 12
1
$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.

user7282
  • 5,106
  • 9
  • 41
  • 72
-3
   $now = date("Y-m-d H:i:s");
   date("Y-m-d H:i:s", strtotime("+1 hours $now"));
Basil Baby
  • 29
  • 2