在对象阵列中查找最接近的日期/时间

时间:2016-10-10 10:32:40

标签: php arrays

我正在寻找一些关于找到最接近的startTime(推荐的startTime)的最佳方法的建议。

到目前为止我得到了什么:

<?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);


?>

印刷阵列:

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
        )

)

我希望它如何运作:

  • 找到meeting_id == 1812的最早开始时间,然后找到最近的下一个会议ID!= 1812.但是,会议ID!= 1812的开始时间需要大于1812年的选定结束时间< / LI>

1 个答案:

答案 0 :(得分:2)

以下函数将您的数组作为第一个参数,会议ID作为第二个参数,然后将最近的会议返回到ID不是$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;
        }
    }
}

给出以下输入(存储在$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
        )

)

并且这样称呼它:

get_nearest_meeting($meetings, 1812);

我们最终得到以下结果:

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
)