BelowZero Posted March 17, 2012 Share Posted March 17, 2012 I'm trying to add some minutes ($interval) to any given time, but my results aren't as expected... <?php $year="2012"; $opentime="09"; $interval="10"; $thedate = "$year:01:01 $opentime:00:00"; $startdate = strtotime($thedate); $date = date("Y-m-d",$startdate); $time = date("g:i a",$startdate); $newtime = strtotime("+$interval", $time); $nexttime = date("g:i a",$newtime); echo $date; echo "<br>"; echo $time; echo "<br>"; echo $nexttime; is returning: 2012-01-01 9:00 am 7:10 pm I was expecting the last to be 9:10 am. Can someone point me in the right direction? Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/259139-adding-minutes-to-time/ Share on other sites More sharing options...
spfoonnewb Posted March 17, 2012 Share Posted March 17, 2012 Here are two examples: <?php $minutes = 10; $time = strtotime('2012-09-10 09:00:00'); $time += 60 * $minutes; echo date('Y-m-d H:i:s', $time); ?> <?php echo date('Y-m-d H:i:s', strtotime('+10 minutes', strtotime('2012-09-10 09:00:00'))); ?> Quote Link to comment https://forums.phpfreaks.com/topic/259139-adding-minutes-to-time/#findComment-1328476 Share on other sites More sharing options...
BelowZero Posted March 17, 2012 Author Share Posted March 17, 2012 spfoonnewb, These examples will work until I try to use variables for the year and hour. I need to write the code using variables ($year, $opentime, $interval) that are coming from another page. Quote Link to comment https://forums.phpfreaks.com/topic/259139-adding-minutes-to-time/#findComment-1328485 Share on other sites More sharing options...
ManiacDan Posted March 17, 2012 Share Posted March 17, 2012 TIME, as computers handle it, is not in the format of 09:15. TIME (like the second argument to date() and strtotime()) is the number of seconds since 01/01/1970. You are trying to give "7:10" when 138348402382 is expected. You have to do this math by hand, there is no way to add minutes and hours in PHP without also giving it a date string. Also, you're using "+10" as your argument to strtotime. 10 what? Strtotime assumes seconds, since as I said above time is measured in seconds. Quote Link to comment https://forums.phpfreaks.com/topic/259139-adding-minutes-to-time/#findComment-1328486 Share on other sites More sharing options...
BelowZero Posted March 17, 2012 Author Share Posted March 17, 2012 Thanks ManiacDan. Here's the code that works in case anyone is interested. <?php $year= "2012"; $opentime= "09"; $interval= "10"; $thedate = "$year:01:01 $opentime:00:00"; $startdate = strtotime($thedate); $date = date("Y-m-d",$startdate); $time = date("g:i a",$startdate); $newtime = strtotime("+$interval minute", $startdate); $nexttime = date("g:i a",$newtime); ?> Quote Link to comment https://forums.phpfreaks.com/topic/259139-adding-minutes-to-time/#findComment-1328505 Share on other sites More sharing options...
ManiacDan Posted March 17, 2012 Share Posted March 17, 2012 If this time ever goes past midnight, it will be wrong on days with daylight savings time. Otherwise it's correct. Quote Link to comment https://forums.phpfreaks.com/topic/259139-adding-minutes-to-time/#findComment-1328513 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.