dougu4027 2016-02-19 20:10
浏览 368
已采纳

Google Calendar API算法可查找特定日期的开放时间段

I'm trying to leverage Google Calendar (and Google Calendar API) for simple scheduling. I'm using PHP, MySQL and google-api-php-client. I'm looking for a algorithm or pseudocode to find open time slots.

My calendar has events for busy times such as when business is closed and for current appointments. I am able to retrieve event start and end times via two function (Events: list and Freebusy: query). Here is a example list of events for today with start time of 2016-02-19T01:00:00-07:00 and end time of 2016-02-19T23:00:00-07:00 (everything for the current day).

Business is Closed from 12:00am to 11:00am
start: "2016-02-19T00:00:00-07:00"
end: "2016-02-19T11:00:00-07:00"

Appointment from 1:30pm to 2:30pm
start: "2016-02-19T13:30:00-07:00"
end: "2016-02-19T14:30:00-07:00"

Appointment from 5:00pm to 7:00pm
start: "2016-02-19T17:00:00-07:00"
end: "2016-02-19T19:00:00-07:00"

Appointment from 9:00pm to 11:00pm
start: "2016-02-19T21:00:00-07:00"
end: "2016-02-19T23:00:00-07:00"

My open time slots based on this example event set is: 11:00am to 1:30pm, 2:30pm to 5:00pm, 7:00pm to 9:00pm and 11:00pm to 12:00am.

  1. How do I find this set of times programmatically?

Ideally I would like to get a full list of events for a given day with one API call and interpret/manipulate the list instead of making many calls with specific start and end times which would give me the information I'm looking for but be very inefficient.

  1. After I find the set, how do I display results in 30 minute increments i.e. available appointment start times are:

11:00,11:30,12:00,12:30,1:00,2:30,3:00,3:30,4:00,4:30,9:00,9:30,10:00,10:30,11:00,11:30

I'm having a hard time wrapping my head around working with dates/time data sets.

Here is an image of the Google Calendar. Date in question is 2/19/2016

Thank you kindly. Any source that is a result of this discussion will be posted for reference.

  • 写回答

2条回答 默认 最新

  • duanping2809 2016-02-19 21:23
    关注

    I would convert the dates to unix timestamps and calculate the difference between the start and end dates. This is how I would accomplish the first part of your question:

    $events = array(
        array(
            'start' => '2016-02-19T00:00:00-07:00',
            'end' => '2016-02-19T11:00:00-07:00'
        ),
        array(
            'start' => '2016-02-19T13:30:00-07:00',
            'end' => '2016-02-19T14:30:00-07:00'
        ),
        array(
            'start' => '2016-02-19T17:00:00-07:00',
            'end' => '2016-02-19T19:00:00-07:00'
        ),
        array(
            'start' => '2016-02-19T21:00:00-07:00',
            'end' => '2016-02-19T23:00:00-07:00'
        )
    );
    
    $free_time_slots = array();
    $count = count($events)-1;
    $i = 0;
    foreach($events as $event) {
        if ($i < $count) {
            $free_time = strtotime($events[$i+1]['start']) - strtotime($event['end']);
            $free_time_slots[] = array(
                'start' => date("F j, Y, g:i a", strtotime($event['end'])),
                'end' => date("F j, Y, g:i a", strtotime($events[$i+1]['start'])),
                'minutes' => $free_time / 60
            );
            $i++;
        }
    }  
    echo '<pre>';
    print_r($free_time_slots);
    

    Which will result in this:

    Array
    (
        [0] => Array
            (
                [start] => February 19, 2016, 11:00 am
                [end] => February 19, 2016, 1:30 pm
                [minutes] => 150
            )
    
        [1] => Array
            (
                [start] => February 19, 2016, 2:30 pm
                [end] => February 19, 2016, 5:00 pm
                [minutes] => 150
            )
    
        [2] => Array
            (
                [start] => February 19, 2016, 7:00 pm
                [end] => February 19, 2016, 9:00 pm
                [minutes] => 120
            )
    
    )
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码