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?

2
  • 1
    strtotime() php.net/manual/en/function.strtotime.php
    – user557846
    Jul 8, 2012 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, 2014 at 14:59

13 Answers 13

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.

3
  • That works, but if i replace the '5' with a variable, it doesnt work properly. strtotime('+$hours hours')
    – Jonah Katz
    Jul 8, 2012 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, 2013 at 7: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 Sep 8, 2022 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


4
  • 3
    Small tyop here, $date->format should probably be $now->format
    – Patrick
    Jan 11, 2015 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, 2021 at 23:55
  • @s.co.tt 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, 2021 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 6, 2021 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
3
  • @RichardDesLonde Probably because everyone copied the OP's code example. :-)
    – Zar
    Aug 14, 2013 at 12:01
  • 1
    This did not work for me, Rodrigo Prazim's answer did.
    – Lucas
    May 25, 2019 at 22:22
  • 1
    A non well formed numeric value encountered - doesn't work
    – user1239087
    Apr 2, 2020 at 0:36
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
0
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";
3
  • bad idea, issues with leap years, leap seconds, daylight savings time etc.
    – user557846
    Jul 8, 2012 at 20:17
  • 1
    @Dagon no, it will calculate properly.
    – casraf
    Jul 8, 2012 at 20:19
  • Leap years are taken care of using this method, daylight saving can be set while using date again
    – Ankit
    Jul 8, 2012 at 20:20
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
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
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
  • 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, 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. Jan 9, 2022 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.

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

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.

-3
   $now = date("Y-m-d H:i:s");
   date("Y-m-d H:i:s", strtotime("+1 hours $now"));

Not the answer you're looking for? Browse other questions tagged or ask your own question.