Google Calendar API算法可查找给定日期的开放时间段 [英] Google Calendar API algorithm to find open time slots for a given day
问题描述
我正在尝试利用Google Calendar(和Google Calendar API)进行简单的计划。我正在使用PHP,MySQL和google-api-php-client。我正在寻找一种算法或伪代码来查找开放时间段。
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.
我的日历中有一些忙碌事件,例如营业时间和当前约会。我能够通过两个函数(事件:列表和忙碌:查询)检索事件的开始和结束时间。这是今天事件的示例列表,其开始时间为2016-02-19T01:00:00-07:00,结束时间为2016-02-19T23:00:00-07:00(当天的所有时间) 。
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).
营业时间为12:00 am至11:00 am
开始: 2016-02-19T00: 00:00-07:00
结尾: 2016-02-19T11:00:00-07:00
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"
从1:30 pm到2:30 pm的约会
开始: 2016-02-19T13:30:00-07:00
结束: 2016-02 -19T14:30: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"
从5:00 pm到7:00 pm的约会
开始: 2016-02-19T17:00:00-07:00
结尾: 2016-02-19T19:00: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"
从9:00 pm到11:00 pm的约会
开始: 2016-02-19T21:00:00-07:00
结尾: 2016-02-19T23: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"
基于此示例事件集的开放时间为:11:00 am至下午1:30 pm、2:30pm至5:00 pm、7:00pm至9:00 pm和11:00 pm至12:00 am。
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.
- 我如何找到这套以编程方式f次?
理想情况下,我想通过一个API调用获取给定日期的事件的完整列表,而不是解释/操作该列表拨打许多具有特定开始和结束时间的电话,这会给我所需的信息,但是效率很低。
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.
- 找到集合后,如何以30分钟为增量显示结果,即可用的约会开始时间为:
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
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.
这里是 Google日历。有问题的日期是2/19/2016
Here is an image of the Google Calendar. Date in question is 2/19/2016
谢谢。
推荐答案
我会将日期转换为unix时间戳并进行计算,以供参考。开始日期和结束日期之间的差额。这就是我要完成的第一部分问题:
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);
这将导致以下结果:
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
)
)
这篇关于Google Calendar API算法可查找给定日期的开放时间段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!