tags:

views:

48

answers:

5

I have two times start_time=2009-12-01 9.30 pm and end_time=2009-12-01 11.30 pm(YYYY-MM-DD).If the user do any activity between these times ie at 2009-12-01 10.30 pm I need to tell him you are not valid to do this activity... The two times 2009-12-01 9.30 pm and 2009-12-01 11.30 pm is taken from database.The time 2009-12-01 10.30 is given by user.

My question is how can I find the time 10.30pm is occured inbetween 9.30pm to 11.30pm...

I do my program in PHP

+1  A: 
$start = strtotime($start_time);
$end = strtotime($end_time);
$user = strtotime($user_time);

if($user > $start && $user < $end) die('You are not allowed');
dfilkovi
+1  A: 

Use strtotime() to convert the strings into Unix seconds. Then a simple

if ($time1 < strtotime(10:30...) && strtotime(10:30...) < $time 2)
{
    //do your stuff;
}
Alex Mcp
whether is it correct both the time you have check it as a <
vinothkumar
A: 

convert "2009-12-01 9.30 pm" and "2009-12-01 11.30 pm" to unix timestamps, then compare those with <:

if ($start <= $event && $event < $end)
    complain();
} else {
    proceed();
}
just somebody
Didn't PHP already have a proper DateTime class?
Joey
I think you lost this: `{`
Otto Allmendinger
I think it's only available for php versions greater or equal to 5.3
AntonioCS
+1  A: 

You can use database query or PHP. In PHP:

$timeStart = strtotime('2009-12-01 11.30');
$timeEnd   = strtotime('2009-12-01 09.30');
$time = strtotime('2009-12-01 10.30');

if($time > $timeStart && $time < $timeEnd) {
   echo "Between given times.";
}
eyazici
A: 

There is no need to do any excessive PHP handling when you can simply use a SQL query:

SELECT 1 FROM your_table_name WHERE '2009-12-01 10:30:00' BETWEEN start_time AND end_time;

This will return 1 or more result(s) if the user activity occurs between any start and end times within the database. Please note you should have a leftmost prefix index on your table for (start_time, end_time).

Just check your query results. If you have a row count greater than or equal to 1 you know that the user activity falls between start and end times in your database.

cballou