zebe Posted July 27, 2006 Share Posted July 27, 2006 Hi,I have a variable that is storing a time value that has been retrieved from a database. It is in the format of: HH:MM:SS. What i need to do is subtract a half hour from this value, does PHP have any way of doing this? I was thinking about using explode to get the hours and seconds and then using conditionals to determine how to reformatt it, but that seems bulky to me... Just wondering if anyone has a more efficient solution. Thanks for the help! Quote Link to comment https://forums.phpfreaks.com/topic/15810-php-and-time-how-to-subtract-time/ Share on other sites More sharing options...
otuatail Posted July 27, 2006 Share Posted July 27, 2006 you may have to convert it to a unix stamp value and subtract 1800 seconds and convert it back. Quote Link to comment https://forums.phpfreaks.com/topic/15810-php-and-time-how-to-subtract-time/#findComment-64683 Share on other sites More sharing options...
kenrbnsn Posted July 27, 2006 Share Posted July 27, 2006 You can use strtotime():[code]<?php$tm = '13:56:32';$ntim1 = date('G:i:s',strtotime($tm . ' -30 minutes'));echo $ntim1;?>[/code]Ken Quote Link to comment https://forums.phpfreaks.com/topic/15810-php-and-time-how-to-subtract-time/#findComment-64686 Share on other sites More sharing options...
Gast Posted July 27, 2006 Share Posted July 27, 2006 [code]<?php$mysqldate = date("H:i:s", strtotime($row['your_mysql_date']));$newdate = date("H:", strtotime($row['your_mysql_date'])) . (date("i:", strtotime($row['your_mysql_date'])) -30) . date("s", strtotime($row['your_mysql_date']));echo $newdate;?>[/code]Try that. Quote Link to comment https://forums.phpfreaks.com/topic/15810-php-and-time-how-to-subtract-time/#findComment-64687 Share on other sites More sharing options...
zebe Posted July 27, 2006 Author Share Posted July 27, 2006 None of these techniques seem to work. When I have 14:00:00 initially, I get 14:30:00, not 13:30:00 as desired... Any other ideas?Thanks again for your help! Quote Link to comment https://forums.phpfreaks.com/topic/15810-php-and-time-how-to-subtract-time/#findComment-64729 Share on other sites More sharing options...
kenrbnsn Posted July 27, 2006 Share Posted July 27, 2006 My (modified) code works fine:[code]<?php$tm = '14:00:00';$ntim1 = date('G:i:s',strtotime($tm . ' -30 minutes'));echo $ntim1;?>[/code]You may not have noticed that I took out the space I originally had after the '-' in the original post.Ken Quote Link to comment https://forums.phpfreaks.com/topic/15810-php-and-time-how-to-subtract-time/#findComment-64733 Share on other sites More sharing options...
zebe Posted July 27, 2006 Author Share Posted July 27, 2006 Perfect! Thank you so much. Quote Link to comment https://forums.phpfreaks.com/topic/15810-php-and-time-how-to-subtract-time/#findComment-64734 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.