如何在上午8:00到晚上8:00之间的15分钟间隔内计算可用时间?

时间:2013-12-14 23:16:32

标签: php mysql sql calculator appointment

我想开发一个iOS应用程序,它可以让您在15分钟的时间间隔内从上午8:00到晚上8:00预约,条件是您的预约时间为1小时30分钟。< / p>

应用程序将下载此次并将其粘贴到tableView中。其他约会的时间(开始,结束时间,持续时间)将存储在MySQL数据库中。我想用PHP或SQL来计算它(我不知道,哪个更好)。

到目前为止,这是我的想法:

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;

}

好吧,这段代码顶部不包含给定的条件,但我想添加它,如果我可以构建一个工作代码。

1 个答案:

答案 0 :(得分:1)

在SQL中,我将时间间隔计算为(我已根据您的要求进行了修改)

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

在我的项目中,我将其用作

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

以后

select * from timetable

希望这会有所帮助