I am doing resource allocation for an event.
My event table for one resource is looks like this:
(Int)---(nvarchar(100)---(datetime)--(datetime)
EventId --- Event --- StartTime --- EndTime
1 / test / 2013-02-20 13:00:00 / 2013-02-20 15:00:00
2 / test2 / 2013-02-20 09:30:00 / 2013-02-20 11:00:00
3 / test3 / 2013-02-25 11:30:00 / 2013-02-25 14:30:00
Now I want to find the total availablity for this resource on one day.
Like on 20th Feb 2013 I want to remove the busy hours from this resource and want to show only available hours for new event.
I am using php and sql server 2008 r2.
It works fine with only one record of a single day. Right now I am using a foreach loop with calculation.
My Code is:
$id = 6;
$cdata = $test->getResourceEvents($id);
$h = "";
$final= array();
foreach($cdata as $c)
{
$sh = $c['starttime']->Format('H'); // Starting hour
$eh = $c['endtime']->Format('H'); // End hour
$hh = $sh;
$final = array();
$sdate = $c['starttime']->Format('Y-m-d');
$edate = $c['endtime']->Format('Y-m-d');
if($edate == $sdate)
{
$dh = $eh-$sh; // Duration
for($i=1;$i<=$dh;$i++)
{
$hh = $hh.",".($sh+$i); // Busy hours
}
$busyhrs[$sdate] = explode(",",$hh);
$final[$sdate] = $busyhrs;
}
else
{
echo "false";
}
}
print_r($final);
and my result is :
Array
(
[2013-02-20] => Array
(
[2013-02-20] => Array
(
[0] => 9
[1] => 10
[2] => 11
)
)
[2013-02-26] => Array
(
[2013-02-26] => Array
(
[0] => 11
[1] => 12
[2] => 13
[3] => 14
)
)
)
First two records have the same dates. but this only calculates 2nd row’s hours. Not calculating first row’s hours that is 13,14,15.
Can anyone please tell me how to match the dates and how to get total busy hours of one date?
I found the solution.
I changed the process of making final array.
Thanks a lot to all for helping me.
I think its simple.
Here is my code.
May be it will be helpful to someone.