Adding 1 hour to time variable

PhpDatetimeTime

Php Problem Overview


I have a time to which I want to add an hour:

$time = '10:09';

I've tried:

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

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

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

But none of the above work.

Php Solutions


Solution 1 - Php

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

Solution 2 - Php

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

Solution 3 - Php

$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

Solution 4 - Php

try this it is worked for me.

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

Solution 5 - Php

2020 Update

It is weird that no one has suggested the OOP way:

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

OR

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

OR

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

Extract it in string with format:

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

Solution 6 - Php

Simple and smart solution:

date("H:i:s", time()+3600);

Solution 7 - Php

You can try this code:

$time = '10:09';

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

Solution 8 - Php

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

Solution 9 - Php

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.

Solution 10 - Php

2021 Update

Worked for me..

$text = str_replace(':PM', '', '19:00:PM');   //19:00:PM  //Removes :PM
$text = str_replace(':AM', '', $text);   //Removes :AM
$time = strtotime($text);   //19:00
$startTime = date("H:i:A", strtotime('- 1 hours', $time));
$endTime = date("H:i:A", strtotime('+ 1 hours', $time));

Output:

echo $startTime; //18:00:PM
echo $endTime;  //20:00:PM

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser849137View Question on Stackoverflow
Solution 1 - Phppaulcol.View Answer on Stackoverflow
Solution 2 - PhpAzam AlviView Answer on Stackoverflow
Solution 3 - PhpFrosty ZView Answer on Stackoverflow
Solution 4 - PhpMayank VadiyaView Answer on Stackoverflow
Solution 5 - PhpAbhay MauryaView Answer on Stackoverflow
Solution 6 - PhpLadislav NěmečekView Answer on Stackoverflow
Solution 7 - Phpbaladeva juganasView Answer on Stackoverflow
Solution 8 - PhpRicardo SouzaView Answer on Stackoverflow
Solution 9 - Phpuser3298040View Answer on Stackoverflow
Solution 10 - PhpjaiView Answer on Stackoverflow