PHP匹配数组中的3个键

Hi I've been struggling with the most efficient way of doing this. I have an array like this

array(3) {
  [0]=>
  object(stdClass)#495 (4) {
    ["year"]=>
    string(4) "2015"
    ["month"]=>
    string(1) "1"
    ["dayofmonth"]=>
    string(2) "29"
    ["posts"]=>
    string(1) "3"
  }
  [1]=>
  object(stdClass)#521 (4) {
    ["year"]=>
    string(4) "2015"
    ["month"]=>
    string(1) "1"
    ["dayofmonth"]=>
    string(2) "28"
    ["posts"]=>
    string(1) "2"
  }
  [2]=>
  object(stdClass)#522 (4) {
    ["year"]=>
    string(4) "2015"
    ["month"]=>
    string(1) "1"
    ["dayofmonth"]=>
    string(2) "25"
    ["posts"]=>
    string(1) "1"
  }
}

And I have the following values

$d = 29, $m = 1, $y = 2015

What I want to do is find which item in the array [0,1,2] is the next date before the date given by my $d, $m and $y variables.

For example, the answer below would be [1]

If the date was $d=28 then it would be [2]

If the day was $d=25 then it would return false.

The issue is the array could cover a number of years so it needs to match exactly all 3 keys and then return the key number after the found key. The array will always be in descending date order

Is this possible?

EDIT:

I am trying to query a list of items on a date basis so it will show

29 JAN 2015

Click NEXT

It will show the next day which if there is an item will just be day minus 1 but if not it will be the day minus [x]

So the list could be

29 JAN 2015
{items}
28 JAN 2015
{items}
25 JAN 2015
{items}
10 DEC 2014
{items}
29 JAN 2014
{items}
...
25 JAN 1999
{items}

and I want to be able to have this so it just shows the most up to date, then someone can click to show the next dates bunch of items.

If there's an easier way to do this via a dynamic mySQL it would be useful

FURTHER EDIT

I can extract just the date too

array(3) {
  [0]=>
  object(stdClass)#495 (2) {
    ["post_date"]=>
    string(19) "2015-01-29 14:23:28"
    ["posts"]=>
    string(1) "3"
  }
  [1]=>
  object(stdClass)#521 (2) {
    ["post_date"]=>
    string(19) "2015-01-28 14:24:05"
    ["posts"]=>
    string(1) "2"
  }
  [2]=>
  object(stdClass)#522 (2) {
    ["post_date"]=>
    string(19) "2015-01-25 14:41:43"
    ["posts"]=>
    string(1) "1"
  }
}

With the view to select the first [20] number of posts and then "next" would load in the next days worth of posts

If your array is sorted then it's actually pretty easy, just loop in reverse order until your search is greater than the current value

function s($array, $y,$m,$d) {
    $array = array_reverse($array);
    $search = new DateTime( "$y-$m-$d" );
    foreach($array as $k=>$v) {
        $current = new DateTime( "{$v->year}-{$v->month}-{$v->dayofmonth}" );
        if ( $current >= $search )
          return $k-1;
    }
    // otherwise return false, you didn't find any correct date
    return false;
}

It seems that regardless, you are going to need to match the object properties in the array. I would suggest something along the lines of:

<?php

$dateList = array(
    (object)array("year"=>2015, "month"=>1, "dayofmonth"=>29, "posts" => 2 ),
    (object)array("year"=>2015, "month"=>1, "dayofmonth"=>28, "posts" => 2 ),
    (object)array("year"=>2015, "month"=>1, "dayofmonth"=>25, "posts" => 1 )
    );

//print_r($dateList);


$d = 28;
$m = 1;
$y = 2015;


function findPrevious($y, $m, $d, $dateList){
    foreach ($dateList as $key=>$object){
        if ($object->year == $y && $object->month == $m && $object->dayofmonth == $d){
            $propose = $key + 1;
            if ($propose > count($dateList)-1 ){
                $propose = NULL;
            }
            return $propose;
        }
    }
    return NULL;

}

print_r(findPrevious($y, $m, $d, $dateList));

Note that you also need to handle the possibility of attempting a non-existent key, in this case, I'm simply returning null. However, it may be more appropriate to throw an exception, or do something else entirely.

A way using SQL Query :

$currentDate = '2015-01-01';

select * 
from items i, (Select created_at from items
               where created_at > $currentDate 
               order by created_at asc limit 1) next_date
where i.created_at >= next_date.created_at   
and i.created_at <= (Select created_at from items 
                     where created_at > next_date.created_at limit 1)