PHPTOM Posted January 9, 2009 Share Posted January 9, 2009 Hi, I am coding a timetable and basically I want to know how to add minutes to 24hour. I have for example 13:00 in the database, and I want to add 120 minutes to this. Can someone assist me in a function to do this please? Thanks Quote Link to comment Share on other sites More sharing options...
bluesoul Posted January 9, 2009 Share Posted January 9, 2009 $newtime = strtotime("+2 hours",$oldtime); Quote Link to comment Share on other sites More sharing options...
PHPTOM Posted January 9, 2009 Author Share Posted January 9, 2009 Sorry doesnt work, it displays 7212. Basically I have: A while, getting the start time from mysql. $starttime = $a['time']; Then I put this $newtime = strtotime("+2 hours",$starttime); and echo $newtime; It displays: 7212 Quote Link to comment Share on other sites More sharing options...
premiso Posted January 9, 2009 Share Posted January 9, 2009 You have to echo it out using [m]date/m] <?php $newtime = strtotime("+2 hours",$a['time']); echo date('H:i', $newtime); ?> Should print it out right. Just remember, we usually provide examples, it is up to you to take those examples and implement them correctly in your code, IE replacing "$oldtime" to be $a['time']. Quote Link to comment Share on other sites More sharing options...
PHPTOM Posted January 9, 2009 Author Share Posted January 9, 2009 Yup, I know about examples. I added that variable to show you better, as you won't know what $a['time'] means. I'm having problems with it not working the correct hour out. <table width="100%" cellpadding="4" cellspacing="0" border="0"> <tr> <td><font class="heading2">Start</font></td> <td><font class="heading2">End</font></td> <td><font class="heading2">Presenter</font></td> </tr> <?PHP $q = mysql_query("SELECT * FROM `timetable` WHERE `day` = '".$_GET['day']."' ORDER BY `time` ASC"); while($a = mysql_fetch_array($q)){ $newtime = strtotime("+2 hours", $a['time']); ?> <tr> <td><?=$a['time'];?></td> <td><?=date('H:i', $newtime);?></td> <td><?=$a[presenter];?></td> </tr> <?PHP } ?> </table> This is what I have dispayed: 17:00 02:00 Tom Any ideas? The 02:00 should show 19:00. Quote Link to comment Share on other sites More sharing options...
premiso Posted January 9, 2009 Share Posted January 9, 2009 Ok, I hate that stupid strtotime function, but it should work in this aspect. <?php $newtime = date("H:i", strtotime($a['time'])+3600*2); // 3600*2 = 2 hours ?> That should work I believe. Quote Link to comment Share on other sites More sharing options...
PHPTOM Posted January 9, 2009 Author Share Posted January 9, 2009 Edit: Works perfectly. I just plain echod new time, instead of using date. Great thanks a lot! Quote Link to comment Share on other sites More sharing options...
premiso Posted January 9, 2009 Share Posted January 9, 2009 Not sure why, I just ran the test on my server doing this: <?php $a['time'] = "17:00"; $newtime = date("H:i", strtotime($a['time'])+3600*2); // 3600*2 = 2 hours echo "The time should be 19:00 and (drum role) the time is: " . $newtime; die(); ?> Output: The time should be 19:00 and (drum role) the time is: 19:00 Quote Link to comment 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.