php - 如何从 8 :00 am to 8:00 pm? 计算 15 分钟间隔内的可用时间

标签 php mysql sql calculator appointment

我想开发一个 iOS 应用程序,它为您提供从上午 8:00 到晚上 8:00 的 15 分钟间隔内的约会时间,条件是您的约会持续时间为 1 小时 30 分钟。

App 这次会下载并将它们粘贴到 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;

}

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

最佳答案

在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

希望对你有帮助

关于php - 如何从 8 :00 am to 8:00 pm? 计算 15 分钟间隔内的可用时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20589366/

相关文章:

sql - 在 SQL Server 2005 中使用 MD5 在 varbinary 上执行校验和文件

Java,保持近乎恒定的SQL连接

php - yii2 advanced 中的公共(public)资源放在哪里?

PHP PDO 获取对象转换为字符串

php - 为整个数据库导入单个 MySQL .sql 文件 |未选择数据库错误

php - 如何使用 whereHas 优化此查询?

MySQL 查询选项定义中的 PHP 下拉框

php - Travis mysql服务报错

java - 将 Java Applet 连接到 MySQL DB - 通信链路故障

mysql - 选择数据时mysql查询中的逻辑错误