我正在尝试利用Google日历(和Google Calendar API)进行简单的安排。我正在使用PHP,MySQL和google-api-php-client。我正在寻找算法或伪代码来查找开放时间段。
我的日历包含忙碌时间的事件,例如业务关闭和当前约会时。我能够通过两个函数(事件:列表和Freebusy:查询)检索事件的开始和结束时间。以下是今日活动的示例列表,其开始时间为2016-02-19T01:00:00-07:00,结束时间为2016-02-19T23:00:00-07:00(当天的所有内容)
营业时间从凌晨12:00到上午11:00关闭
开始:“2016-02-19T00:00:00-07:00”
结束:“2016-02-19T11:00:00-07:00”
预约从下午1:30到下午2:30
开始:“2016-02-19T13:30:00-07:00”
结束:“2016-02-19T14:30:00-07:00”
从下午5:00到下午7:00预约
开始:“2016-02-19T17:00:00-07:00”
结束:“2016-02-19T19:00:00-07:00”
从晚上9点到晚上11点预约
开始:“2016-02-19T21:00:00-07:00”
结束:“2016-02-19T23:00:00-07:00”
基于此示例事件集的我的开放时间段为:上午11:00至下午1:30,下午2:30至下午5:00,晚上7:00至晚上9:00以及晚上11:00至凌晨12:00。
理想情况下,我希望通过一次API调用获取某一天的事件的完整列表并解释/操作列表,而不是使用特定的开始和结束时间进行多次调用,这将为我提供我正在寻找的信息但效率很低。
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
我正忙着处理日期/时间数据集。
以下是Google Calendar的图片。有问题的日期是2016年2月19日
谢天谢地。将发布此讨论结果的任何来源以供参考。
答案 0 :(得分:3)
我会将日期转换为unix时间戳并计算开始日期和结束日期之间的差异。这就是我将如何完成你问题的第一部分:
$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
)
)
答案 1 :(得分:1)
这应该可以完成工作。您可以在要搜索的时间范围内遍历每个约会时段。在该循环内,您可以检查每个当前约会以查看是否存在冲突。此代码将在每次迭代后回显打开的约会,但您可能希望将它们推送到数组中,以便您可以在需要的地方使用它们。
// Set timezone = UTC for consistency
date_default_timezone_set("UTC");
// Range to search for open times in.
$start_time = strtotime("2016-02-19T00:00:00-07:00");
$end_time = strtotime("2016-02-19T23:59:59-07:00");
// Test data
$events = array(
"event_1" => array(
"start" => "2016-02-19T00:00:00-07:00",
"end" => "2016-02-19T11:00:00-07:00"
),
"event_2" => array(
"start" => "2016-02-19T13:30:00-07:00",
"end" => "2016-02-19T14:30:00-07:00"
),
"event_3" => array(
"start" => "2016-02-19T17:00:00-07:00",
"end" => "2016-02-19T19:00:00-07:00"
),
"event_4" => array(
"start" => "2016-02-19T21:00:00-07:00",
"end" => "2016-02-19T23:00:00-07:00"
),
);
// Kick off first appt time at beginning of the day.
$appt_start_time = $start_time;
// Loop through each appt slot in the search range.
while ($appt_start_time < $end_time) {
// Add 29:59 to the appt start time so we know where the appt will end.
$appt_end_time = ($appt_start_time + 1799);
// For each appt slot, loop through the current appts to see if it falls
// in a slot that is already taken.
$slot_available = true;
foreach ($events as $event => $times) {
$this_start = strtotime($times['start']);
$this_end = strtotime($times['end']);
// If the appt start time or appt end time falls on a current appt, slot is taken.
if (($appt_start_time >= $this_start && $appt_start_time < $this_end) ||
($appt_end_time >= $this_start && $appt_end_time < $this_end)) {
$slot_available = false;
break; // No need to continue if it's taken.
}
}
// If we made it through all appts and the slot is still available, it's an open slot.
if ($slot_available) {
$date = new DateTime();
$date->setTimestamp($appt_start_time);
$date->setTimezone(new DateTimeZone("America/Denver"));
echo "Open appt at: ". $date->format("Y-m-d h:i:s-e") ."<br />";
}
// + 30 minutes
$appt_start_time += (60 * 30);
}
这导致:
Open appt at: 2016-02-19 11:00:00-America/Denver
Open appt at: 2016-02-19 11:30:00-America/Denver
Open appt at: 2016-02-19 12:00:00-America/Denver
Open appt at: 2016-02-19 12:30:00-America/Denver
Open appt at: 2016-02-19 01:00:00-America/Denver
Open appt at: 2016-02-19 02:30:00-America/Denver
Open appt at: 2016-02-19 03:00:00-America/Denver
Open appt at: 2016-02-19 03:30:00-America/Denver
Open appt at: 2016-02-19 04:00:00-America/Denver
Open appt at: 2016-02-19 04:30:00-America/Denver
Open appt at: 2016-02-19 07:00:00-America/Denver
Open appt at: 2016-02-19 07:30:00-America/Denver
Open appt at: 2016-02-19 08:00:00-America/Denver
Open appt at: 2016-02-19 08:30:00-America/Denver
Open appt at: 2016-02-19 11:00:00-America/Denver
Open appt at: 2016-02-19 11:30:00-America/Denver