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.
Dharman♦
28k21 gold badges75 silver badges127 bronze badges
asked Dec 18, 2011 at 13:43
1
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
answered Dec 18, 2011 at 13:46
paulcol.paulcol.
2,7211 gold badge20 silver badges18 bronze badges
2
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']];
answered Jul 2, 2014 at 9:21
Azam AlviAzam Alvi
6,6087 gold badges60 silver badges84 bronze badges
1
$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
answered Dec 18, 2011 at 13:49
Frosty ZFrosty Z
21.6k11 gold badges81 silver badges110 bronze badges
try this it is worked for me.
$time="10:09";
$time = date['H:i', strtotime[$time.'+1 hour']];
echo $time;
answered Mar 25, 2016 at 6:53
Mayank VadiyaMayank Vadiya
1,4172 gold badges20 silver badges31 bronze badges
3
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']];
Dharman♦
28k21 gold badges75 silver badges127 bronze badges
answered Dec 16, 2019 at 10:38
Abhay MauryaAbhay Maurya
11k6 gold badges44 silver badges59 bronze badges
Simple and smart solution:
date["H:i:s", time[]+3600];
answered Dec 1, 2015 at 21:50
You can try this code:
$time = '10:09';
echo date[ 'H:i', strtotime[ '+1 hour' , strtotime[$time] ] ];
Hamed B
1391 silver badge11 bronze badges
answered Apr 6, 2016 at 9:12
I had a similar problem and the fix was to say “hours” instead of “hour”.
answered May 26 at 16:27
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.
answered Feb 11, 2014 at 16:17
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
answered Mar 20, 2021 at 6:13