Page 1 of 2

Time comparisons

Posted: Sat Jan 17, 2009 12:24 pm
by juxp00
Hello. I need to do the following:

-I have a field called "starttime" that is populated with a time via an upload form.
-For each record, I need to determine what time group "starttime" comes into, it is either:

1.Between: 06:00:00 and 12:00:00
2.Between: 12:00:00 and 17:00:00
3.After: 17:00:00

Once I have worked out which bracket "starttime" falls under, I need to populate an additional field called "timebracket" with these values:

If 1. Time Zone 1
If 2. Time zone 2
If 3. Time zone 3

I can't think whether it is the correct way to do this on form upload, or after the data has been entered. Perhaps if all times went into "starttime", could I then work with these to put into a group via time conditionals the output a variable with the value Time Zone X?

Any ideas much appreciated.

Thanks

Re: Time comparisons

Posted: Sat Jan 17, 2009 1:27 pm
by Mark Baker

Code: Select all

 
$hour = date('G',$starttime);
$timezone = 0;
if (($hour >= 6) && ($hour < 12)) {
   $timezone = 1;
} elseif (($hour >= 12) && ($hour < 17)) {
   $timezone = 2;
} elseif ($hour > 17) {
   $timezone = 3;
}
$timebracket = 'Time zone '.$timezone;
 
You don't say what you want to happen between midnight and 6am

Re: Time comparisons

Posted: Sat Jan 17, 2009 1:50 pm
by juxp00
Thankyou very much I will try this out now. Between midnight and 6am nothing happens, I don't need to use a time zone for that space.

Thanks

Re: Time comparisons

Posted: Wed Jan 21, 2009 3:18 pm
by juxp00
The results I get from this code puts everything into "Time Zone 3", as it calculates every $hour as 18.

What exactly is this part doing?
$hour = date('G',$starttime);
As this is the part where $hour and $starttime don't seem to be comparing properly.

Thanks :D

Re: Time comparisons

Posted: Wed Jan 21, 2009 3:24 pm
by Burrito
$hour = date('G',$starttime); is just returning the hour based on a time that is being passed to the function see date() for examples of parameters you can use.

keep in mind that $startime in that example would have to be a unix timestamp NOT a regular date/time. you can use strtotime() to convert it if you need to.

Re: Time comparisons

Posted: Fri Jan 23, 2009 8:16 am
by juxp00
Here's my test to see if I can return $starttime as a Unix timestamp, then do the function:

Code: Select all

 
$hour = date('G',strtotime($starttime));
$timezone = 0;
if (($hour >= 6) && ($hour < 12)) {
   $timezone = 1;
} elseif (($hour >= 12) && ($hour < 17)) {
   $timezone = 2;
} elseif ($hour > 17) {
   $timezone = 3;
}
$timebracket = 'Time zone '.$timezone;
 
$query = "SELECT starttime FROM events";
$result = mysql_query($query) or die(mysql_error());
 
echo "<table border='1'><tr>";
for($i = 0; $i < mysql_num_fields($result); $i++){
    echo "<th>".mysql_field_name($result, $i)."</th>";
    echo "<th>Time Bracket</th>";
}
echo "</tr>";
while($row = mysql_fetch_array($result)){
    echo "<tr>";
    for($i = 0; $i < mysql_num_fields($result); $i++){
        echo "<td>". $row[$i] ."</td>";
        echo "<td>". $timebracket ."</td>";
    }
    echo "</tr>";
} 
echo "</table>";
 
Again I get Timezone 3 returned for all.

echo strtotime($row[$i]) converts to Unix, but when I check the Unix timestamp on a conversion site, the time value is different.

Any ideas???

Thanks

Re: Time comparisons

Posted: Mon Jan 26, 2009 8:44 am
by juxp00
I have got this working now, thanks for your help

Code: Select all

 
$abc = strtotime($row['starttime']);
$hour = date("G:i",$abc);
$timezone = 0;
if (($hour >= 6) && ($hour < 12)) {
   $timezone = 1;
} elseif (($hour >= 12) && ($hour < 17)) {
   $timezone = 2;
} elseif ($hour > 17) {
   $timezone = 3;
}
$timebracket = 'Time zone '.$timezone;
}
 
Excuse the meaningless variable, just a test.

Re: Time comparisons

Posted: Mon Jan 26, 2009 8:57 am
by Mark Baker
juxp00 wrote:I have got this working now, thanks for your help
It will still work with this:

Code: Select all

$hour = date("G:i",$abc);
but only because PHP will convert the string value with a colon in that you're generating here to a numeric for the if test comparison. Why not give it a straight numeric value in the first place, without any spurious colons.

Code: Select all

$hour = date("G",$abc);

Re: Time comparisons

Posted: Mon Jan 26, 2009 9:34 am
by juxp00
Because with this in mind:

1.Between: 06:00:00 and 12:00:00
2.Between: 12:00:00 and 17:00:00
3.After: 17:00:00

For example if a time is 13:30, the without the ":i", I just got a "13" returned, which put the time into the wrong bracket.

Re: Time comparisons

Posted: Mon Jan 26, 2009 10:53 am
by Mark Baker
juxp00 wrote:For example if a time is 13:30, the without the ":i", I just got a "13" returned, which put the time into the wrong bracket.
Yes you would get 13 returned from a time of 13:30.

And using your if tests, which test will be fulfilled by a value of 13?

Code: Select all

 
if (($hour >= 6) && ($hour < 12)) {
   $timezone = 1;
} elseif (($hour >= 12) && ($hour < 17)) {
   $timezone = 2;
} elseif ($hour > 17) {
   $timezone = 3;
}
 
Your if test actually ignores the minutes completely, and only looks at the hour

Note, you are actually testing for:
1.Between: 06:00:00 and 11:59:59.99
2.Between: 12:00:00 and 17:59:59.99
3.After: 17:00:00

Incidentally, it should be:

Code: Select all

} elseif ($hour >= 17) {

Re: Time comparisons

Posted: Mon Jan 26, 2009 1:54 pm
by juxp00
Thanks for this - very useful.
And using your if tests, which test will be fulfilled by a value of 13?
timezone 2 right?
} elseif ($hour >= 17) {
Thanks.

Any clues as to how to modify this to test for minutes as well?

Cheers

Re: Time comparisons

Posted: Mon Jan 26, 2009 4:04 pm
by Mark Baker
juxp00 wrote:timezone 2 right?
Right! It's correct with just the hours because your timezone breaks are all hour based, so adding the colon and minutes is just extra overhead
juxp00 wrote:Any clues as to how to modify this to test for minutes as well?

Code: Select all

 
$abc = strtotime($row['starttime']);
$hour = date("G",$abc);
$minutes = date("i",$abc);
$timezone = 0;
if ((($hour >= 9) && ($hour < 12)) || (($hour == 12) && ($minutes < 30))) {
   $timezone = 1;
} elseif ((($hour == 12) && ($minutes >= 30)) || (($hour > 12) && ($hour < 17)) || (($hour == 17) && ($minutes < 30))) {
   $timezone = 2;
} elseif ((($hour == 17) && ($minutes >= 30)) || (($hour > 17) && ($hour < 20))) {
   $timezone = 3;
}
 
Timezone 1 is 9:00:00.00 -> 12:29:59.99
Timezone 2 is 12:30:00.00 -> 17:29:59.99
Timezone 3 is 17:30:00.00 -> 19:59:59.99

Re: Time comparisons

Posted: Mon Jan 26, 2009 4:39 pm
by VladSun

Code: Select all

 
$zones = array(
    'zone1' =>  array('9:00',   '13:00'),
    'zone2' =>  array('13:00',  '17:30'),
    'zone3' =>  array('17:30',  '20:00'),
);
 
function getZone($zones, $time)
{
    $_time = date('H:i', strtotime($time));
    
    foreach ($zones as $zone => $interval)
        if ($_time >= date('H:i', strtotime($interval[0])) && $_time < date('H:i', strtotime($interval[1])))        
            return $zone;
 
    return false;
}
 
$time = '17:56';
 
if ($zone = getZone($zones, $time))
    echo $zone;
else
    echo 'Undefined zone!';

Re: Time comparisons

Posted: Mon Jan 26, 2009 4:43 pm
by VladSun
Even better:

Code: Select all

$zones = array(
    'zone0' =>  array('',       '9:00'),
    'zone1' =>  array('9:00',   '13:00'),
    'zone2' =>  array('13:00',  '17:30'),
    'zone3' =>  array('17:30',  '20:00'),
    'zone4' =>  array('20:00',  ''),
);
 
function getZone($zones, $time)
{
    $_time = date('H:i', strtotime($time));
    
    foreach ($zones as $zone => $interval)
        if (    
                (empty($interval[0]) || $_time >= date('H:i', strtotime($interval[0]))) 
            && 
                (empty($interval[1]) || $_time < date('H:i', strtotime($interval[1])))
            )       
            return $zone;
 
    return false;
}
 
$time = '8:56';
 
if ($zone = getZone($zones, $time))
    echo $zone;
else
    echo 'Undefined zone!';

Re: Time comparisons

Posted: Mon Jan 26, 2009 5:16 pm
by juxp00
Thanks very much Mark , excellent.

VladSun - this looks very cool, but I have no ideas how I would use it for what I need, have you got an example of using it in the context of assigning a variable to a zone and inserting it into a DB, or using it to query entries already in a DB?

Many thanks