How to calculate available times in a 15 Minutes Interval from 8:00 am to 8:00 pm?

849 views Asked by At

I'd like to develop an iOS Application, which gives you available times for an appointment in a 15 Minutes Interval from 8:00 am to 8:00 pm with the condition that your appointment will have an 1h 30min duration.

The App will download this times and will paste them into a tableView. Times of other appointments (beginning, end time, duration) will be stored in a MySQL-Database. I want to calculate it with PHP or SQL (I don't know, what is better).

This is my idea, so far:

function easyfunction($day, $cutter, $open, $closed)    {

    //Create new Array
    $frei    = array();

    //Calculate times for the $frei-Array
    for($time = $open; $time > $closed; $time=$time + date_create_from_format('H:i', 0:15);)    {
        array_push($frei, $time);
    }


    //MySQL-Request
    $connect = mysqli_connect("host", "DB", "Password")or die("Fehler beim Verbinden mit der Datenbank");
    mysqli_select_db("Appointments")or die("Database doesnt exist");
    $sql = "SELECT * FROM termine WHERE friseuse=$cutter AND date=$day";
    $ergebnis = mysqli_query($sql);
    while($row = mysqli_fetch_array($ergebnis)){

        //Write Appointment and duration in variables
        $datetime = $row->datetime;
        $duration = $row->duration;

        //Calculate Ending
        $terminende = $datetime + $duration;

        // Create Search Array
        $search = array();

        //Filter all values from $frei
        $search = array_search($datumzeit < $frei, $frei);
        $search = array_search($ende > $frei , $frei);
        unset($frei[$search]);
}

//Return all times
return $frei;

}

Well, this code doesn't contain the given condition on the top, but I want to add it, if I can build a working code.

1

There are 1 answers

0
Sushil On

In SQL I have calculated time interval as(i have modified according to your requirement)

declare @time datetime
declare @Etime datetime
declare @Interval datetime

set @time='2013-12-18 08:00:00.000'

While @time<'2013-12-18 16:00:00.000' 
Begin
    set @Etime=convert(varchar(25), dateadd(mi,90,@time))
    set @Interval = convert(varchar(25), dateadd(mi,15,@Etime)) 

    select @time as StartTime, @Etime as EndTime, @Interval as Interval

    set @Time = @Interval
End

In my project I have used it as

declare @time datetime
declare @Etime datetime
declare @Interval datetime

set @time='2013-12-18 08:00:00.000'

IF OBJECT_ID('dbo.timetable', 'U') IS NOT NULL
  DROP TABLE dbo.timetable 

create table timetable  (StartTime datetime, EndTime datetime, Interval datetime)

While @time<'2013-12-18 16:00:00.000' 
Begin
    set @Etime=convert(varchar(25), dateadd(mi,90,@time))
    set @Interval = convert(varchar(25), dateadd(mi,15,@Etime)) 

    insert into timetable values(@time, @Etime, @Interval)

    set @Time = @Interval
End

and later

select * from timetable

hope this helps