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
8
  • 1
    You can sort with the startTime and take the first row. For the sorting sort-multi-dimensional-array-by-specific-key Commented Oct 10, 2016 at 10:34
  • Did you try to write some code yourself? Commented Oct 10, 2016 at 10:34
  • @Dekel Yes I'll provide you with that now. Commented Oct 10, 2016 at 10:34
  • Can you provide a sample testcase ? Commented Oct 10, 2016 at 10:37
  • Check this for sorting with date stackoverflow.com/questions/2910611/… Commented Oct 10, 2016 at 10:38

1 Answer 1

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
)
Sign up to request clarification or add additional context in comments.

8 Comments

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?
Okay, so you wish to find the earliest start time for meeting_id == 1812, and then find the closest next meeting whose ID != 1812?
Exactly that :)
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
@ChrisBeckett Please see the edited answer. It should work as you expect now.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.