doukezi4606 2013-12-14 23:16
浏览 78

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

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条回答 默认 最新

  • douxiexie3574 2013-12-18 11:03
    关注

    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

    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?