LLeoun Posted March 26, 2009 Share Posted March 26, 2009 Dear people, I would like to have a form in my page where people could enter events and the time when they will take place. If the time of the event is the current time (the event is happening) I would like to show a page with details and extra info. How can I do that?? I was thinking in a form and a MySQL table with the following fields: start_hour end_hour start_minute end_minute event But I don't know how to match current time and time of event. I mean, imagine right now is 22:30 and there's a concert that started at 22:00 and ends at 22:45 .. I would like to be showing a page related to that concert with extra info .. don't know how to make it happen .. please help. Thanks a ton in advance!! Link to comment https://forums.phpfreaks.com/topic/151210-load-a-page-when-current-time-matches-with-entered-time/ Share on other sites More sharing options...
unska Posted March 26, 2009 Share Posted March 26, 2009 Hey, you can get the time difference with the following function: function get_time_difference($start, $end) { $uts['start'] = strtotime( $start ); $uts['end'] = strtotime( $end ); if( $uts['start']!==-1 && $uts['end']!==-1 ) { if( $uts['end'] >= $uts['start'] ) { $diff = $uts['end'] - $uts['start']; if( $days=intval((floor($diff/86400))) ) $diff = $diff % 86400; if( $hours=intval((floor($diff/3600))) ) $diff = $diff % 3600; if( $minutes=intval((floor($diff/60))) ) $diff = $diff % 60; $diff = intval( $diff ); return( array('days'=>$days, 'hours'=>$hours, 'minutes'=>$minutes, 'seconds'=>$diff) ); } else { trigger_error( "Ending date/time is earlier than the start date/time", E_USER_WARNING ); } } else { trigger_error( "Invalid date/time data detected", E_USER_WARNING ); } return( false ); } And this is how you call the function: $diff=@get_time_difference("00:00:00","23:45:00") Link to comment https://forums.phpfreaks.com/topic/151210-load-a-page-when-current-time-matches-with-entered-time/#findComment-794315 Share on other sites More sharing options...
LLeoun Posted March 26, 2009 Author Share Posted March 26, 2009 Thanks unska, but I'm still a newbie, I don't get how can I use the time difference for my program .. need some more explanation sorry Link to comment https://forums.phpfreaks.com/topic/151210-load-a-page-when-current-time-matches-with-entered-time/#findComment-794325 Share on other sites More sharing options...
kittrellbj Posted March 26, 2009 Share Posted March 26, 2009 If you use the above function and call it using $diff=@get_time_difference("00:00:00","23:45:00") It will return a difference of (I believe) 23 hrs., 45 minutes (or 15 minutes perhaps, didn't read the entire function). So, it's obviously not the same time or within the bounds of the if statement, so it would not bring up the event page yet. Link to comment https://forums.phpfreaks.com/topic/151210-load-a-page-when-current-time-matches-with-entered-time/#findComment-794326 Share on other sites More sharing options...
unska Posted March 26, 2009 Share Posted March 26, 2009 Just make the MySQL table you described earlier (remember to add ID to it). ID start_hour end_hour start_minute end_minute event Then return the data from the MySQL database and insert it to the function. $diff=@get_time_difference($start_hour.":".$start_minute.":00",$end_hour.":".$end_minute.":00") Link to comment https://forums.phpfreaks.com/topic/151210-load-a-page-when-current-time-matches-with-entered-time/#findComment-794328 Share on other sites More sharing options...
unska Posted March 26, 2009 Share Posted March 26, 2009 If you use the above function and call it using $diff=@get_time_difference("00:00:00","23:45:00") It will return a difference of (I believe) 23 hrs., 45 minutes (or 15 minutes perhaps, didn't read the entire function). So, it's obviously not the same time or within the bounds of the if statement, so it would not bring up the event page yet. True, now the event can be calculated with some basic math. Link to comment https://forums.phpfreaks.com/topic/151210-load-a-page-when-current-time-matches-with-entered-time/#findComment-794330 Share on other sites More sharing options...
thebadbad Posted March 26, 2009 Share Posted March 26, 2009 It's much simpler to just save the start and end time as Unix timestamps and then compare with the current timestamp. Just require the user to enter a date and time for the start and end of the event, convert them to timestamps with strtotime(), and then compare like this: <?php //let's say the users supply you with dates and times in standard format "yyyy-mm-dd hh:mm:ss" $start = '2009-03-26 10:00:00'; $end = '2009-03-26 22:00:00'; //if now (time()) is after $start and before $end if (time() > strtotime($start) && time() < strtotime($end)) { //the event is currently happening } ?> Link to comment https://forums.phpfreaks.com/topic/151210-load-a-page-when-current-time-matches-with-entered-time/#findComment-794335 Share on other sites More sharing options...
unska Posted March 26, 2009 Share Posted March 26, 2009 It's much simpler to just save the start and end time as Unix timestamps and then compare with the current timestamp. Just require the user to enter a date and time for the start and end of the event, convert them to timestamps with strtotime(), and then compare like this: <?php //let's say the users supply you with dates and times in standard format "yyyy-mm-dd hh:mm:ss" $start = '2009-03-26 10:00:00'; $end = '2009-03-26 22:00:00'; //if now (time()) is after $start and before $end if (time() > strtotime($start) && time() < strtotime($end)) { //the event is currently happening } ?> I agree, you should go with time(). Link to comment https://forums.phpfreaks.com/topic/151210-load-a-page-when-current-time-matches-with-entered-time/#findComment-794336 Share on other sites More sharing options...
LLeoun Posted March 26, 2009 Author Share Posted March 26, 2009 Thanks guys! You're awesome! Link to comment https://forums.phpfreaks.com/topic/151210-load-a-page-when-current-time-matches-with-entered-time/#findComment-794352 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.