Adding 1 hour to time variable

Asked by At

This simple thing has wasted hours of my time trying to get it working, still havnt worked it out...

i have

$time = '10:09';

I want to add an hour to that...

So, I've tried:

$time = strtotime('+1 hour');

strtotime('+1 hour', $time);

$time = date('H:i', strtotime('+1 hour'));

None of the above worked.

Can you guys help me out?

Thanks.

10 Answers

68
paulcol. On Best Solutions

Worked for me..

$timestamp = strtotime('10:09') + 60*60;

$time = date('H:i', $timestamp);

echo $time;//11:09

Explanation:

strtotime('10:09') creates a numerical timestamp in seconds, something like 1510450372. Simply add or remove the amount of seconds you need and use date() to convert it back into a human readable format.

$timestamp = strtotime('10:09') + 60*60; // 10:09 + 1 hour
$timestamp = strtotime('10:09') + 60*60*2; // 10:09 + 2 hours
$timestamp = strtotime('10:09') - 60*60; // 10:09 - 1 hour

time() also creates a numerical timestamp but for right now. You can use it in the same way.

$timestamp = time() + 60*60; // now + 1 hour
1
rcdmk On

You can use:

$time = strtotime("10:09") + 3600;
echo date('H:i', $time);

Or date_add: http://www.php.net/manual/en/datetime.add.php

14
Frosty Z On
$time = '10:09';
$timestamp = strtotime($time);
$timestamp_one_hour_later = $timestamp + 3600; // 3600 sec. = 1 hour

// Formats the timestamp to HH:MM => outputs 11:09.
echo strftime('%H:%M', $timestamp_one_hour_later);
// As crolpa suggested, you can also do
// echo date('H:i', $timestamp_one_hour_later);

Check PHP manual for strtotime(), strftime() and date() for details.

BTW, in your initial code, you need to add some quotes otherwise you will get PHP syntax errors:

$time = 10:09; // wrong syntax
$time = '10:09'; // syntax OK

$time = date(H:i, strtotime('+1 hour')); // wrong syntax
$time = date('H:i', strtotime('+1 hour')); // syntax OK
0
user3298040 On

Beware of adding 3600!! may be a problem on day change because of unix timestamp format uses moth before day.

e.g. 2012-03-02 23:33:33 would become 2014-01-13 13:00:00 by adding 3600 better use mktime and date functions they can handle this and things like adding 25 hours etc.

18
Azam Alvi On

You can do like this

    echo date('Y-m-d H:i:s', strtotime('4 minute'));
    echo date('Y-m-d H:i:s', strtotime('6 hour'));
    echo date('Y-m-d H:i:s', strtotime('2 day'));
0
Ladislav Němeček On

Simple and smart solution:

date("H:i:s", time()+3600);
7
Mayank Vadiya On

try this it is worked for me.

$time="10:09";
$time = date('H:i', strtotime($time.'+1 hour'));
echo $time;
0
baladeva juganas On

You can try this code:

$time = '10:09';

echo date( 'H:i', strtotime( '+1 hour' , strtotime($time) ) );
5
Akbor On

for this problem please follow bellow code:

$time= '10:09';
$new_time=date('H:i',strtotime($time.'+ 1 hour'));
echo $new_time;`
// now output will be: 11:09
1
Learner On

2019 Update

It is weird that no one suggested OOP way:

$date = new \DateTime(); //now
$date->add(new \DateInterval('PT3600S'));//add 3600s

OR

$date = new \DateTime(); //now
$date->add(new \DateInterval('PT60M'));//add 60 min

OR

$date = new \DateTime(); //now
$date->add(new \DateInterval('P1H'));//add 1 hour

Extract it in string with format:

var_dump($date->format('Y-m-d H:i:s'));

I hope it helps