从开始和结束日期开始的PHP免费时间段 [英] PHP free time blocks from start and end dates

查看:108
本文介绍了从开始和结束日期开始的PHP免费时间段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图纯粹从MySQL查询中获取这些空闲时间块,但是我无法构造一个可以处理已有数据的查询.我选择仅从查询中获取startend日期,并计算出PHP中的空闲时间块.但是,这也证明是困难的.

I attempted to get these free time blocks purely from a MySQL query, but I was not able to construct a query that worked with the data that have. I have opted to simply get the start and end dates from the query and work out the free time blocks in PHP. However this is also proving difficult.

我有以下数组(仅举一个例子,日期和时间会有所不同):

I have the following array (Just an example, days and times will vary):

$meeting = array(array('start' => '2016-11-14 16:00:00',
                       'end'   => '2016-11-14 16:30:00'
                      ),
                 array('start' => '2016-11-14 16:45:00',
                       'end'   => '2016-11-14 20:00:00'
                      ),
                 array('start' => '2016-11-14 14:00:00',
                       'end'   => '2016-11-14 15:00:00'
                      ),
                 array('start' => '2016-11-14 13:00:00',
                       'end'   => '2016-11-14 14:00:00'
                      ),
                 array('start' => '2016-11-11 15:20:00',
                       'end'   => '2016-11-11 16:00:00'
                      ),
                 array('start' => '2016-11-11 14:00:00',
                       'end'   => '2016-11-11 15:00:00'
                      ),
                 array('start' => '2016-11-07 07:00:00',
                       'end'   => '2016-11-09 15:00:00'
                      )
               );

我想结束的是一天中每个空闲时间段的开始和结束时间的天数,每天仅在09:00和17:00之间,不包括周末.因此,例如,上面的会议时间数组应产生以下空闲时间数组:

What I would like to end up with is an array of days with start and end times for each free time block on that day, only between 09:00 and 17:00 on each day, excluding weekends. So for example, the meeting times array above should produce the following array of free time:

$daily_free = array('2016-11-09' => array(array('free_start' => '15:00:00',
                                                'free_end'   => '17:00:00'
                                               )
                                         ),
                    '2016-11-10' => array(array('free_start' => '09:00:00',
                                                'free_end'   => '17:00:00'
                                               )
                                         ),
                    '2016-11-11' => array(array('free_start' => '09:00:00',
                                                'free_end'   => '14:00:00'
                                               ),
                                          array('free_start' => '15:00:00',
                                                'free_end'   => '15:20:00'
                                               ),
                                          array('free_start' => '16:00:00',
                                                'free_end'   => '17:00:00'
                                               )
                                         ),
                    '2016-11-14' => array(array('free_start' => '09:00:00',
                                                'free_end'   => '13:00:00'
                                               ),
                                          array('free_start' => '15:00:00',
                                                'free_end'   => '16:00:00'
                                               ),
                                          array('free_start' => '16:30:00',
                                                'free_end'   => '16:45:00'
                                               )
                                         )
                   );

如您所见,只有几天有任何空闲时间出现在数组中,所以11月7日和8日没有空闲时间,因此根本没有出现.另外,11月12日和13日也没有出现在空闲时间数组中,这是因为它们是在周末.但是,如果12月和13日不是周末,则它们应在空闲时间数组中显示为整天的空闲时间(09:00至17:00).

As you can see only days with any free time appear in the array, the 7th and 8th of November have no free time so they do not appear at all. Also, the 12th and 13th of November do not appear in the free time array, this is because they fall on the weekend. However, if the 12th and 13th were not weekends, they should appear in the free time array as full days of free time (09:00 to 17:00).

我可以想到的唯一方法之一是创建一个每天的数组,并且在其中有一个从09:00到17:00的5分钟增量数组,其值为NULL.然后遍历此数组,检查每个日期和5分钟的增量是否在第一个数组的开始和结束时间之内,如果有,则将其标记为忙.然后,我可以再次遍历并获取每天空值的开始和结束时间吗?这似乎不是一个很好的解决方案,而且如果会议少于几分钟,也会失败.

One of the only ways I can think to achieve this is to create an array of each day, and within it have an array of 5 minute increments from 09:00 to 17:00 with a NULL value. Then loop through this array checking if each date and 5 minute increment is within the start and end time in the first array, and mark it as busy if it is. I can then loop through again and get the start and end times of the null values on each day? This doesn't seem like a great solution, and also fails if a meeting is less than minutes.

我认为目前我还看不到树木茂盛的树木,并且可能已经过分思考了.

I don't think I can see the wood for trees at this point, and may be over thinking it.

还有其他解决方法吗?

SQL小提琴供任何想通过SQL尝试的人

SQL Fiddle for anyone who wants to try via SQL

推荐答案

我要解决的方法是尝试思考问题,就像您是从材料中雕刻出一些东西一样.我将从充满空闲时间的一天开始[09-17],然后循环约会.对于每个约会,您要么拆分现有的空闲时间(然后按开始日期重新排序),要么更改范围.如果时间不重叠,则容易得多.如果约会超出了空闲时间范围,则可以忽略它.

They way I would work through it would be to try thinking of the problem like you are carving something out of a material. I'd start with a day full of free time [09 - 17] then loop over the appointments. For every appointment you are either splitting the existing free time (then re-ordering by start date) or changing the range. It's a lot easier if the times don't overlap. If the appointment is outside of the free time range you can ignore it.

{----}  {--------} Free Time Range
           [---]   Appointment in the middle - you need to split

{----}  {--------} Free Time Range
        [---]      Appointment at the beginning - change the start date

{----}  {--------} Free Time Range
             [---] Appointment at the end - change the end date

{----}  {--------} Free Time Range
        [--------] Appointment fills the whole spot - delete

这篇关于从开始和结束日期开始的PHP免费时间段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆