1

I'm looking for some advise on what would be the best way to find the closest startTime (recommended startTime).

What I've got so far:

<?php
$array = array(    array("meeting_id" => "1812",  
                         "startTime" => "2016-10-07 14:30:00", 
                         "endTime" => "2016-10-07 14:35:00"),
                   array("meeting_id" => "1812",  
                         "startTime" => "2016-10-07 14:35:00", 
                         "endTime" => "2016-10-07 14:40:00"),
                   array("meeting_id" => "1812",  
                         "startTime" => "2016-10-07 14:40:00", 
                         "endTime" => "2016-10-07 14:45:00"),
                   array("meeting_id" => "1813",  
                         "startTime" => "2016-10-07 15:05:00", 
                         "endTime" => "2016-10-07 15:10:00"),
                   array("meeting_id" => "1813",  
                         "startTime" => "2016-10-07 15:10:00", 
                         "endTime" => "2016-10-07 15:15:00"),
                   array("meeting_id" => "1813",  
                         "startTime" => "2016-10-07 15:20:00", 
                         "endTime" => "2016-10-07 15:25:00"),
    );

arsort($array);
$firstTime = $array[0];
foreach($array as $key){
    if($firstTime["startTime"] > $key["endTime"]){
        // Do something in here.
    }
}

print_r($array);


?>

Printed Array:

Array
(
    [5] => Array
        (
            [meeting_id] => 1813
            [startTime] => 2016-10-07 15:20:00
            [endTime] => 2016-10-07 15:25:00
        )

    [4] => Array
        (
            [meeting_id] => 1813
            [startTime] => 2016-10-07 15:10:00
            [endTime] => 2016-10-07 15:15:00
        )

    [3] => Array
        (
            [meeting_id] => 1813
            [startTime] => 2016-10-07 15:05:00
            [endTime] => 2016-10-07 15:10:00
        )

    [2] => Array
        (
            [meeting_id] => 1812
            [startTime] => 2016-10-07 14:40:00
            [endTime] => 2016-10-07 14:45:00
        )

    [1] => Array
        (
            [meeting_id] => 1812
            [startTime] => 2016-10-07 14:35:00
            [endTime] => 2016-10-07 14:40:00
        )

    [0] => Array
        (
            [meeting_id] => 1812
            [startTime] => 2016-10-07 14:30:00
            [endTime] => 2016-10-07 14:35:00
        )

)

How I would expect it to work:

  • Find the earliest start time for meeting_id == 1812, and then find the closest next meeting whose ID != 1812. However the start time of those meeting ID != 1812 needs to be greater than the selected end time of 1812
YaBCK
  • 2,949
  • 4
  • 32
  • 61

1 Answers1

2

The following function will take your array as the first argument, and the Meeting ID as the second, and will in turn return the nearest meeting to the earliest meeting whose ID is not $id:

function get_nearest_meeting($meetings, $id)
{
    // Start by sorting the meetings:
    function sorter($a, $b)
    {
        return strtotime($a['startTime']) - strtotime($b['startTime']);
    }
    usort($meetings, 'sorter');

    foreach( $meetings as $meeting )
    {
        if( $meeting['meeting_id'] == $id)
        {
            $earliest = strtotime( $meeting['endTime'] );
            break;
        }
    }

    // Now loop over again and get the next meeting:
    foreach( $meetings as $meeting )
    {
        if( $meeting['meeting_id'] != $id && strtotime($meeting['startTime']) > $earliest)
        {
            return $meeting;
        }
    }
}

Given the following input (stored in $meetings):

Array
(
    [0] => Array
        (
            [errors] => 0
            [meeting_id] => 1812
            [id] => 31305
            [startTime] => 2016-10-07 14:00:00
            [endTime] => 2016-10-07 14:10:00
            [grade_id] => 87
        )

    [1] => Array
        (
            [errors] => 0
            [meeting_id] => 1813
            [id] => 31305
            [startTime] => 2016-10-07 14:10:00
            [endTime] => 2016-10-07 14:20:00
            [grade_id] => 87
        )

    [2] => Array
        (
            [errors] => 0
            [meeting_id] => 1812
            [id] => 31305
            [startTime] => 2016-10-07 14:10:00
            [endTime] => 2016-10-07 14:20:00
            [grade_id] => 87
        )

    [3] => Array
        (
            [errors] => 0
            [meeting_id] => 1812
            [id] => 31305
            [startTime] => 2016-10-07 14:20:00
            [endTime] => 2016-10-07 14:30:00
            [grade_id] => 87
        )

    [4] => Array
        (
            [errors] => 0
            [meeting_id] => 1813
            [id] => 31305
            [startTime] => 2016-10-07 14:20:00
            [endTime] => 2016-10-07 14:30:00
            [grade_id] => 87
        )

    [5] => Array
        (
            [errors] => 0
            [meeting_id] => 1815
            [id] => 31305
            [startTime] => 2016-10-07 14:30:00
            [endTime] => 2016-10-07 14:40:00
            [grade_id] => 87
        )

    [6] => Array
        (
            [errors] => 0
            [meeting_id] => 1812
            [id] => 31305
            [startTime] => 2016-10-07 14:30:00
            [endTime] => 2016-10-07 14:40:00
            [grade_id] => 87
        )

    [7] => Array
        (
            [errors] => 0
            [meeting_id] => 1813
            [id] => 31305
            [startTime] => 2016-10-07 14:30:00
            [endTime] => 2016-10-07 14:40:00
            [grade_id] => 87
        )

    [8] => Array
        (
            [errors] => 0
            [meeting_id] => 1815
            [id] => 31305
            [startTime] => 2016-10-07 14:40:00
            [endTime] => 2016-10-07 14:50:00
            [grade_id] => 87
        )

    [9] => Array
        (
            [errors] => 0
            [meeting_id] => 1813
            [id] => 31305
            [startTime] => 2016-10-07 14:40:00
            [endTime] => 2016-10-07 14:50:00
            [grade_id] => 87
        )

)

And calling it like this:

get_nearest_meeting($meetings, 1812);

We end up with the following result:

Array
(
    [errors] => 0
    [meeting_id] => 1813
    [id] => 31305
    [startTime] => 2016-10-07 14:10:00
    [endTime] => 2016-10-07 14:20:00
    [grade_id] => 87
)
BenM
  • 52,573
  • 26
  • 113
  • 168
  • Hey BenM - Thanks for this. However could it work a little differently, so when the `id` is passed it will check the earliest `startTime` then it will check the rest to find the closest start time which is greater than the `endTime` of the chosen earliest time based on the passed `id`? – YaBCK Oct 10 '16 at 11:00
  • Okay, so you wish to find the earliest start time for `meeting_id` == `1812`, and then find the closest *next* meeting whose ID != `1812`? – BenM Oct 10 '16 at 11:01
  • Exactly that :) – YaBCK Oct 10 '16 at 11:01
  • However the next meet whose ID != 1812 has to have a start time greater than the end time in the selected meeting with ID == 1812 – YaBCK Oct 10 '16 at 11:04
  • @ChrisBeckett Please see the edited answer. It should work as you expect now. – BenM Oct 10 '16 at 11:11
  • No problem, glad to help! – BenM Oct 10 '16 at 11:18
  • Sorry to keep bothering you, have you an idea how I would adapt what you have to have something like.... If I wanted to book 4 people, so the first person would be the earliest time selected on `meeting_id == 1812` then 2nd person would be `meeting_id == 1813` then 3rd person would be `meeting_id == 1814` and 4th person would be `meeting_id == 1815`? – YaBCK Oct 10 '16 at 11:26
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/125327/discussion-between-benm-and-chris-beckett). – BenM Oct 10 '16 at 11:26