Time comparisons
Moderator: General Moderators
Time comparisons
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
-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
-
Mark Baker
- Forum Regular
- Posts: 710
- Joined: Thu Oct 30, 2008 6:24 pm
Re: Time comparisons
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;
Re: Time comparisons
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
Thanks
Re: Time comparisons
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?
Thanks
What exactly is this part doing?
As this is the part where $hour and $starttime don't seem to be comparing properly.$hour = date('G',$starttime);
Thanks
Re: Time comparisons
$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.
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
Here's my test to see if I can return $starttime as a Unix timestamp, then do the function:
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
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>";
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
I have got this working now, thanks for your help
Excuse the meaningless variable, just a test.
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;
}
-
Mark Baker
- Forum Regular
- Posts: 710
- Joined: Thu Oct 30, 2008 6:24 pm
Re: Time comparisons
It will still work with this:juxp00 wrote:I have got this working now, thanks for your help
Code: Select all
$hour = date("G:i",$abc);Code: Select all
$hour = date("G",$abc);Re: Time comparisons
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.
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.
-
Mark Baker
- Forum Regular
- Posts: 710
- Joined: Thu Oct 30, 2008 6:24 pm
Re: Time comparisons
Yes you would get 13 returned from a time of 13:30.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.
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;
}
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
Thanks for this - very useful.
Any clues as to how to modify this to test for minutes as well?
Cheers
timezone 2 right?And using your if tests, which test will be fulfilled by a value of 13?
Thanks.} elseif ($hour >= 17) {
Any clues as to how to modify this to test for minutes as well?
Cheers
-
Mark Baker
- Forum Regular
- Posts: 710
- Joined: Thu Oct 30, 2008 6:24 pm
Re: Time comparisons
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 overheadjuxp00 wrote:timezone 2 right?
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 2 is 12:30:00.00 -> 17:29:59.99
Timezone 3 is 17:30:00.00 -> 19:59:59.99
Re: Time comparisons
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!';There are 10 types of people in this world, those who understand binary and those who don't
Re: Time comparisons
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!';There are 10 types of people in this world, those who understand binary and those who don't
Re: Time comparisons
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
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