Chris Beckett Chris Beckett - 1 month ago 6
PHP Question

Best way to determine which time is the best to push into an array?

I'm just looking for some advise on how I would work out the best possible meeting slots. So that pupils meetings don't overlap and aren't too far away. I'm trying to work out recommended times, so that a parent can see the best slots to book so that they're not waiting round.

E.g how it should work:

Parent 1:

Child 1 and Child 2


Meeting 1:

Slot `2016-10-07 14:00:00` (free)(duration 10mins)(Child 1)
Slot `2016-10-07 14:10:00` (free)(duration 10mins)(Child 2)
Slot `2016-10-07 14:20:00` (free)(duration 10mins)


Meeting 2:

Slot `2016-10-07 14:15:00` (free)(duration 5mins)
Slot `2016-10-07 14:20:00` (free)(duration 5mins)(Child 1)
Slot `2016-10-07 14:25:00` (free)(duration 5mins)(Child 2)


Meeting 3:

Slot `2016-10-07 14:10:00` (free)(duration 10mins)
Slot `2016-10-07 14:20:00` (free)(duration 10mins)
Slot `2016-10-07 14:30:00` (free)(duration 10mins)(Child 1)
Slot `2016-10-07 14:40:00` (free)(duration 10mins)(Child 2)


What would be the best way to check that the time hasn't already been stored into an array and if the time has already been stored then check the next time to see if the time can fit with the other times based on the duration?

You don't have to use my data, an example would be as useful. Thanks in advance.

Data I've got so far:

Pupils:

Array
(
[0] => pupil Object (
[id] => 48307
[school_id] => 1
[grade_id] => 87
[firstname] => Callum
[surname] => Lacey
[gender] => M
[username] => callum
[password] => callum
[avatar] => 003_large.gif
[uuid] =>
[active] => 1
[deleted] => 0
[inserted] => 2015-09-14 10:57:29
[updated] => 2016-09-15 13:09:07
[login] => 2016-09-26 15:07:15
)

[1] => pupil Object (
[id] => 79571
[school_id] => 1
[grade_id] => 87
[firstname] => Daniel
[surname] => Lacey
[gender] => M
[username] => daniellacey
[password] => password123
[avatar] => 003_large.gif
[uuid] =>
[active] => 1
[deleted] => 0
[inserted] => 2016-07-06 10:51:26
[updated] => 2016-09-15 12:23:53
[login] => 2016-09-23 13:09:28
)
)


Loaded all meeting slots which are free:

Array
(
[0] => meeting Object (
[errors] => 0
[id] => 1812
[title] => Parent Evening 1 - 2016-10-07 14:00
[slot_id] => 31305
[grade_id] => 87
[startTime] => 2016-10-07 14:00:00
[endTime] => 2016-10-07 14:10:00
)

[1] => meeting Object (
[errors] => 0
[id] => 1815
[title] => Parent Evening 3 - 2016-10-07 14:00
[slot_id] => 31317
[grade_id] => 87
[startTime] => 2016-10-07 14:30:00
[endTime] => 2016-10-07 14:40:00
)
[2] => meeting Object (
[errors] => 0
[id] => 1815
[title] => Parent Evening 3 - 2016-10-07 14:00
[slot_id] => 31318
[grade_id] => 87
[startTime] => 2016-10-07 14:40:00
[endTime] => 2016-10-07 14:50:00
)
[3] => meeting Object (
[errors] => 0
[id] => 1815
[title] => Parent Evening 3 - 2016-10-07 14:00
[slot_id] => 31319
[grade_id] => 87
[startTime] => 2016-10-07 14:50:00
[endTime] => 2016-10-07 15:00:00
)
[4] => meeting Object (
[errors] => 0
[id] => 1816
[title] => 2016-10-07 14:15
[slot_id] => 31321
[grade_id] => 87
[startTime] => 2016-10-07 14:15:00
[endTime] => 2016-10-07 14:20:00
)
[5] => meeting Object (
[errors] => 0
[id] => 1815
[title] => Parent Evening 3 - 2016-10-07 14:00
[slot_id] => 31320
[grade_id] => 87
[startTime] => 2016-10-07 15:00:00
[endTime] => 2016-10-07 15:10:00
)
[6] => meeting Object (
[errors] => 0
[id] => 1813
[title] => Parent Evening 2 - 2016-10-07 14:00
[slot_id] => 31312
[grade_id] => 87
[startTime] => 2016-10-07 14:40:00
[endTime] => 2016-10-07 14:50:00
)
[7] => meeting Object (
[errors] => 0
[id] => 1813
[title] => Parent Evening 2 - 2016-10-07 14:00
[slot_id] => 31311
[grade_id] => 87
[startTime] => 2016-10-07 14:30:00
[endTime] => 2016-10-07 14:40:00
)
[8] => meeting Object (
[errors] => 0
[id] => 1812
[title] => Parent Evening 1 - 2016-10-07 14:00
[slot_id] => 31306
[grade_id] => 87
[startTime] => 2016-10-07 14:10:00
[endTime] => 2016-10-07 14:20:00
)
[9] => meeting Object (
[errors] => 0
[id] => 1812
[title] => Parent Evening 1 - 2016-10-07 14:00
[slot_id] => 31307
[grade_id] => 87
[startTime] => 2016-10-07 14:20:00
[endTime] => 2016-10-07 14:30:00
)
[10] => meeting Object (
[errors] => 0
[id] => 1812
[title] => Parent Evening 1 - 2016-10-07 14:00
[slot_id] => 31308
[grade_id] => 87
[startTime] => 2016-10-07 14:30:00
[endTime] => 2016-10-07 14:40:00
)
[11] => meeting Object (
[errors] => 0
[id] => 1813
[title] => Parent Evening 2 - 2016-10-07 14:00
[slot_id] => 31310
[grade_id] => 87
[startTime] => 2016-10-07 14:20:00
[endTime] => 2016-10-07 14:30:00
)
[12] => meeting Object (
[errors] => 0
[id] => 1813
[title] => Parent Evening 2 - 2016-10-07 14:00
[slot_id] => 31309
[grade_id] => 87
[startTime] => 2016-10-07 14:10:00
[endTime] => 2016-10-07 14:20:00
)
[13] => meeting Object (
[errors] => 0
[id] => 1816
[title] => 2016-10-07 14:15
[slot_id] => 31322
[grade_id] => 87
[startTime] => 2016-10-07 14:20:00
[endTime] => 2016-10-07 14:25:00
)
[14] => meeting Object (
[errors] => 0
[id] => 1816
[title] => 2016-10-07 14:15
[slot_id] => 31323
[grade_id] => 87
[startTime] => 2016-10-07 14:25:00
[endTime] => 2016-10-07 14:30:00
)
[15] => meeting Object (
[errors] => 0
[id] => 1819
[title] => 2016-10-07 14:20
[slot_id] => 31341
[grade_id] => -1
[startTime] => 2016-10-07 15:10:00
[endTime] => 2016-10-07 15:20:00
)
[16] => meeting Object (
[errors] => 0
[id] => 1819
[title] => 2016-10-07 14:20
[slot_id] => 31342
[grade_id] => -1
[startTime] => 2016-10-07 15:20:00
[endTime] => 2016-10-07 15:30:00
)
[17] => meeting Object (
[errors] => 0
[id] => 1819
[title] => 2016-10-07 14:20
[slot_id] => 31343
[grade_id] => -1
[startTime] => 2016-10-07 15:30:00
[endTime] => 2016-10-07 15:40:00
)
[18] => meeting Object (
[errors] => 0
[id] => 1819
[title] => 2016-10-07 14:20
[slot_id] => 31345
[grade_id] => -1
[startTime] => 2016-10-07 15:50:00
[endTime] => 2016-10-07 16:00:00
)
[19] => meeting Object (
[errors] => 0
[id] => 1819
[title] => 2016-10-07 14:20
[slot_id] => 31344
[grade_id] => -1
[startTime] => 2016-10-07 15:40:00
[endTime] => 2016-10-07 15:50:00
)
[20] => meeting Object (
[errors] => 0
[id] => 1819
[title] => 2016-10-07 14:20
[slot_id] => 31340
[grade_id] => -1
[startTime] => 2016-10-07 15:00:00
[endTime] => 2016-10-07 15:10:00
)
[21] => meeting Object (
[errors] => 0
[id] => 1819
[title] => 2016-10-07 14:20
[slot_id] => 31339
[grade_id] => -1
[startTime] => 2016-10-07 14:50:00
[endTime] => 2016-10-07 15:00:00
)
[22] => meeting Object (
[errors] => 0
[id] => 1816
[title] => 2016-10-07 14:15
[slot_id] => 31324
[grade_id] => 87
[startTime] => 2016-10-07 14:30:00
[endTime] => 2016-10-07 14:35:00
)
[23] => meeting Object (
[errors] => 0
[id] => 1816
[title] => 2016-10-07 14:15
[slot_id] => 31325
[grade_id] => 87
[startTime] => 2016-10-07 14:35:00
[endTime] => 2016-10-07 14:40:00
)
[24] => meeting Object (
[errors] => 0
[id] => 1819
[title] => 2016-10-07 14:20
[slot_id] => 31336
[grade_id] => -1
[startTime] => 2016-10-07 14:20:00
[endTime] => 2016-10-07 14:30:00
)
[25] => meeting Object (
[errors] => 0
[id] => 1819
[title] => 2016-10-07 14:20
[slot_id] => 31338
[grade_id] => -1
[startTime] => 2016-10-07 14:40:00
[endTime] => 2016-10-07 14:50:00
)
[26] => meeting Object (
[errors] => 0
[id] => 1819
[title] => 2016-10-07 14:20
[slot_id] => 31337
[grade_id] => -1
[startTime] => 2016-10-07 14:30:00
[endTime] => 2016-10-07 14:40:00
)
)


Using pupils and loaded meeting slots, push slot time for meeting for each pupil and meeting:

Array
(
[2016-10-07 14:00:00] => Array (
[0] => meeting Object (
[errors] => 0
[pupil_id] => 48307
[fullname] => Callum Lacey
[school_id] => 1
[meeting_title] => Parent Evening 1
[meeting_id] => 1812
[slot_id] => 87
[startTime] => 2016-10-07 14:00:00
[endTime] => 2016-10-07 14:10:00
[duration] => 600
)
[1] => meeting Object (
[errors] => 0
[pupil_id] => 79571
[fullname] => Daniel Lacey
[school_id] => 1
[meeting_title] => Parent Evening 1
[meeting_id] => 1812
[slot_id] => 87
[startTime] => 2016-10-07 14:10:00
[endTime] => 2016-10-07 14:20:00
[duration] => 600
)
[2] => meeting Object (
[errors] => 0
[pupil_id] => 48307
[fullname] => Callum Lacey
[school_id] => 1
[meeting_title] => 2016-10-07 14:15
[meeting_id] => 1816
[slot_id] => 87
[startTime] => 2016-10-07 14:15:00
[endTime] => 2016-10-07 14:20:00
[duration] => 300
)
[3] => meeting Object (
[errors] => 0
[pupil_id] => 79571
[fullname] => Daniel Lacey
[school_id] => 1
[meeting_title] => Parent Evening 2
[meeting_id] => 1813
[slot_id] => 87
[startTime] => 2016-10-07 14:20:00
[endTime] => 2016-10-07 14:30:00
[duration] => 600
)
[4] => meeting Object (
[errors] => 0
[pupil_id] => 79571
[fullname] => Daniel Lacey
[school_id] => 1
[meeting_title] => 2016-10-07 14:15
[meeting_id] => 1816
[slot_id] => 87
[startTime] => 2016-10-07 14:25:00
[endTime] => 2016-10-07 14:30:00
[duration] => 300
)
[5] => meeting Object (
[errors] => 0
[pupil_id] => 48307
[fullname] => Callum Lacey
[school_id] => 1
[meeting_title] => Parent Evening 3
[meeting_id] => 1815
[slot_id] => 87
[startTime] => 2016-10-07 14:30:00
[endTime] => 2016-10-07 14:40:00
[duration] => 600
)
[6] => meeting Object (
[errors] => 0
[pupil_id] => 48307
[fullname] => Callum Lacey
[school_id] => 1
[meeting_title] => Parent Evening 2
[meeting_id] => 1813
[slot_id] => 87
[startTime] => 2016-10-07 14:40:00
[endTime] => 2016-10-07 14:50:00
[duration] => 600
)
[7] => meeting Object (
[errors] => 0
[pupil_id] => 79571
[fullname] => Daniel Lacey
[school_id] => 1
[meeting_title] => Parent Evening 3
[meeting_id] => 1815
[slot_id] => 87
[startTime] => 2016-10-07 14:50:00
[endTime] => 2016-10-07 15:00:00
[duration] => 600
)
)

[2016-10-07 14:10:00] => Array (
[0] => meeting Object (
[errors] => 0
[pupil_id] => 79571
[fullname] => Daniel Lacey
[school_id] => 1
[meeting_title] => Parent Evening 1
[meeting_id] => 1812
[slot_id] => 87
[startTime] => 2016-10-07 14:00:00
[endTime] => 2016-10-07 14:10:00
[duration] => 600
)

[1] => meeting Object (
[errors] => 0
[pupil_id] => 48307
[fullname] => Callum Lacey
[school_id] => 1
[meeting_title] => Parent Evening 1
[meeting_id] => 1812
[slot_id] => 87
[startTime] => 2016-10-07 14:10:00
[endTime] => 2016-10-07 14:20:00
[duration] => 600
)
[2] => meeting Object (
[errors] => 0
[pupil_id] => 48307
[fullname] => Callum Lacey
[school_id] => 1
[meeting_title] => 2016-10-07 14:15
[meeting_id] => 1816
[slot_id] => 87
[startTime] => 2016-10-07 14:15:00
[endTime] => 2016-10-07 14:20:00
[duration] => 300
)
[3] => meeting Object (
[errors] => 0
[pupil_id] => 79571
[fullname] => Daniel Lacey
[school_id] => 1
[meeting_title] => Parent Evening 2
[meeting_id] => 1813
[slot_id] => 87
[startTime] => 2016-10-07 14:20:00
[endTime] => 2016-10-07 14:30:00
[duration] => 600
)
[4] => meeting Object (
[errors] => 0
[pupil_id] => 79571
[fullname] => Daniel Lacey
[school_id] => 1
[meeting_title] => 2016-10-07 14:15
[meeting_id] => 1816
[slot_id] => 87
[startTime] => 2016-10-07 14:25:00
[endTime] => 2016-10-07 14:30:00
[duration] => 300
)
[5] => meeting Object (
[errors] => 0
[pupil_id] => 48307
[fullname] => Callum Lacey
[school_id] => 1
[meeting_title] => Parent Evening 3
[meeting_id] => 1815
[slot_id] => 87
[startTime] => 2016-10-07 14:30:00
[endTime] => 2016-10-07 14:40:00
[duration] => 600
)
[6] => meeting Object (
[errors] => 0
[pupil_id] => 48307
[fullname] => Callum Lacey
[school_id] => 1
[meeting_title] => Parent Evening 2
[meeting_id] => 1813
[slot_id] => 87
[startTime] => 2016-10-07 14:40:00
[endTime] => 2016-10-07 14:50:00
[duration] => 600
)
[7] => meeting Object (
[errors] => 0
[pupil_id] => 79571
[fullname] => Daniel Lacey
[school_id] => 1
[meeting_title] => Parent Evening 3
[meeting_id] => 1815
[slot_id] => 87
[startTime] => 2016-10-07 14:50:00
[endTime] => 2016-10-07 15:00:00
[duration] => 600
)
)

[2016-10-07 14:20:00] => Array (
[0] => meeting Object (
[errors] => 0
[pupil_id] => 79571
[fullname] => Daniel Lacey
[school_id] => 1
[meeting_title] => Parent Evening 1
[meeting_id] => 1812
[slot_id] => 87
[startTime] => 2016-10-07 14:00:00
[endTime] => 2016-10-07 14:10:00
[duration] => 600
)
[1] => meeting Object (
[errors] => 0
[pupil_id] => 79571
[fullname] => Daniel Lacey
[school_id] => 1
[meeting_title] => Parent Evening 2
[meeting_id] => 1813
[slot_id] => 87
[startTime] => 2016-10-07 14:10:00
[endTime] => 2016-10-07 14:20:00
[duration] => 600
)
[2] => meeting Object (
[errors] => 0
[pupil_id] => 48307
[fullname] => Callum Lacey
[school_id] => 1
[meeting_title] => 2016-10-07 14:15
[meeting_id] => 1816
[slot_id] => 87
[startTime] => 2016-10-07 14:15:00
[endTime] => 2016-10-07 14:20:00
[duration] => 300
)
[3] => meeting Object (
[errors] => 0
[pupil_id] => 48307
[fullname] => Callum Lacey
[school_id] => 1
[meeting_title] => Parent Evening 1
[meeting_id] => 1812
[slot_id] => 87
[startTime] => 2016-10-07 14:20:00
[endTime] => 2016-10-07 14:30:00
[duration] => 600
)
[4] => meeting Object (
[errors] => 0
[pupil_id] => 79571
[fullname] => Daniel Lacey
[school_id] => 1
[meeting_title] => 2016-10-07 14:15
[meeting_id] => 1816
[slot_id] => 87
[startTime] => 2016-10-07 14:25:00
[endTime] => 2016-10-07 14:30:00
[duration] => 300
)
[5] => meeting Object (
[errors] => 0
[pupil_id] => 48307
[fullname] => Callum Lacey
[school_id] => 1
[meeting_title] => Parent Evening 3
[meeting_id] => 1815
[slot_id] => 87
[startTime] => 2016-10-07 14:30:00
[endTime] => 2016-10-07 14:40:00
[duration] => 600
)
[6] => meeting Object (
[errors] => 0
[pupil_id] => 48307
[fullname] => Callum Lacey
[school_id] => 1
[meeting_title] => Parent Evening 2
[meeting_id] => 1813
[slot_id] => 87
[startTime] => 2016-10-07 14:40:00
[endTime] => 2016-10-07 14:50:00
[duration] => 600
)
[7] => meeting Object (
[errors] => 0
[pupil_id] => 79571
[fullname] => Daniel Lacey
[school_id] => 1
[meeting_title] => Parent Evening 3
[meeting_id] => 1815
[slot_id] => 87
[startTime] => 2016-10-07 14:50:00
[endTime] => 2016-10-07 15:00:00
[duration] => 600
)
)

[2016-10-07 14:30:00] => Array (
[0] => meeting Object (
[errors] => 0
[pupil_id] => 79571
[fullname] => Daniel Lacey
[school_id] => 1
[meeting_title] => Parent Evening 1
[meeting_id] => 1812
[slot_id] => 87
[startTime] => 2016-10-07 14:00:00
[endTime] => 2016-10-07 14:10:00
[duration] => 600
)
[1] => meeting Object (
[errors] => 0
[pupil_id] => 79571
[fullname] => Daniel Lacey
[school_id] => 1
[meeting_title] => Parent Evening 2
[meeting_id] => 1813
[slot_id] => 87
[startTime] => 2016-10-07 14:10:00
[endTime] => 2016-10-07 14:20:00
[duration] => 600
)
[2] => meeting Object (
[errors] => 0
[pupil_id] => 48307
[fullname] => Callum Lacey
[school_id] => 1
[meeting_title] => 2016-10-07 14:15
[meeting_id] => 1816
[slot_id] => 87
[startTime] => 2016-10-07 14:15:00
[endTime] => 2016-10-07 14:20:00
[duration] => 300
)
[3] => meeting Object (
[errors] => 0
[pupil_id] => 48307
[fullname] => Callum Lacey
[school_id] => 1
[meeting_title] => Parent Evening 2
[meeting_id] => 1813
[slot_id] => 87
[startTime] => 2016-10-07 14:20:00
[endTime] => 2016-10-07 14:30:00
[duration] => 600
)
[4] => meeting Object (
[errors] => 0
[pupil_id] => 79571
[fullname] => Daniel Lacey
[school_id] => 1
[meeting_title] => 2016-10-07 14:15
[meeting_id] => 1816
[slot_id] => 87
[startTime] => 2016-10-07 14:25:00
[endTime] => 2016-10-07 14:30:00
[duration] => 300
)
[5] => meeting Object (
[errors] => 0
[pupil_id] => 48307
[fullname] => Callum Lacey
[school_id] => 1
[meeting_title] => Parent Evening 1
[meeting_id] => 1812
[slot_id] => 87
[startTime] => 2016-10-07 14:30:00
[endTime] => 2016-10-07 14:40:00
[duration] => 600
)
[6] => meeting Object (
[errors] => 0
[pupil_id] => 48307
[fullname] => Callum Lacey
[school_id] => 1
[meeting_title] => Parent Evening 3
[meeting_id] => 1815
[slot_id] => 87
[startTime] => 2016-10-07 14:40:00
[endTime] => 2016-10-07 14:50:00
[duration] => 600
)
[7] => meeting Object (
[errors] => 0
[pupil_id] => 79571
[fullname] => Daniel Lacey
[school_id] => 1
[meeting_title] => Parent Evening 3
[meeting_id] => 1815
[slot_id] => 87
[startTime] => 2016-10-07 14:50:00
[endTime] => 2016-10-07 15:00:00
[duration] => 600
)
)
)

Answer

If anybody would like to see how I resolved this problem please check the following link:

Fix to the question