AbydosGater Posted September 8, 2007 Share Posted September 8, 2007 Hi guys, I need to work out how many minutes there is until the next hour.. Server time that is. I have a cron that updates user information.. And i would like to display a message to say how in many minutes the update will be. I have looked around but cant seam to find any methods of doing this. Do any of you know how? I would be very greatfull. Thank you Andy Dam guys haha.. As soon as i post i think of a way. Im going to use date() to figure out how many minutes have passed since last hour.. like 31 minutes.. and then take that away from 60 to give me whats left. Sorry for wasting your time. If you know of any other methods though please tell me. Andy Quote Link to comment https://forums.phpfreaks.com/topic/68476-solved-how-many-minutes-till-next-hour/ Share on other sites More sharing options...
wildteen88 Posted September 8, 2007 Share Posted September 8, 2007 Use date to get the current minutes and seconds. Then you can take the current minute away from 60 to get the remaining minutes, you can do the same with the seconds too. Heres an example. <?php // get the current minute and second using date $date = date("i:s"); // explode against the semi-colon to get the minute and seconds separately list($cur_min, $cur_sec) = explode(':', $date); // calculate remaining minutes and secounds $mins_left = ($cur_min == 59) ? 0 : 60 - $cur_min; $secs_left = 60 - $cur_sec; // echo the time remaining sentance echo 'There is ' . ($mins_left) . ' minutes and ' . $secs_left . ' seconds left until the next hour'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/68476-solved-how-many-minutes-till-next-hour/#findComment-344243 Share on other sites More sharing options...
AbydosGater Posted September 8, 2007 Author Share Posted September 8, 2007 Hey Wildteen, Thanks for the quick reply. I am going to use this method One question though.. I have seen code .. Like yours.. Using "?" and other in brackets. Would you be able to explain the line: <?php $mins_left = ($cur_min == 59) ? 0 : 60 - $cur_min; ?> To me please.. Why are you using comparrison operators ( the ==) on 59 and the ? puts me off. I have seen code like this around.. But never understood it. Andy Quote Link to comment https://forums.phpfreaks.com/topic/68476-solved-how-many-minutes-till-next-hour/#findComment-344252 Share on other sites More sharing options...
wildteen88 Posted September 8, 2007 Share Posted September 8, 2007 Its called a ternary operator . Its an inline version of an if/else statement. The quoted line you posted above basically means this: if($cur_min == 59) { $mins_left = 0; // true } else { $mins_left = 60 - $cur_min // false } format of the ternary operator: (expression) ? TRUE : FALSE Quote Link to comment https://forums.phpfreaks.com/topic/68476-solved-how-many-minutes-till-next-hour/#findComment-344255 Share on other sites More sharing options...
AbydosGater Posted September 8, 2007 Author Share Posted September 8, 2007 Wow.. Three years in PHP and im still learning every day. Thank you. This is very interesting.. Will come in helpful Andy Quote Link to comment https://forums.phpfreaks.com/topic/68476-solved-how-many-minutes-till-next-hour/#findComment-344259 Share on other sites More sharing options...
sneamia Posted September 8, 2007 Share Posted September 8, 2007 Wow.. Three years in PHP and im still learning every day. I look in SMF code for cool things to do. So did you get the code to work or do you still need help? Quote Link to comment https://forums.phpfreaks.com/topic/68476-solved-how-many-minutes-till-next-hour/#findComment-344266 Share on other sites More sharing options...
AbydosGater Posted September 8, 2007 Author Share Posted September 8, 2007 Sneamia: Interesting Idea .. I have it all working. Thank you Quote Link to comment https://forums.phpfreaks.com/topic/68476-solved-how-many-minutes-till-next-hour/#findComment-344273 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.