Jump to content

Load a page when current time matches with entered time


LLeoun

Recommended Posts

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!!

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")

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.

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")

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.

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
}
?>

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().

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.