1

I am looking for two values in an array:

  • last array value from 08/29/2013 08:00 to 08/29/2013 13:00
  • first array value from 08/29/2013 12:00 to 08/29/2013 17:00

Here is the array:

$dates = array(
    '08/29/2013 08:00',
    '08/29/2013 08:10',
    '08/29/2013 08:11',
    '08/29/2013 12:20',
    '08/29/2013 12:21',
    '08/29/2013 12:21',
    '08/29/2013 17:30',
); // etc.
10
  • That last entry btw, doesn't need a comma. Commented Aug 29, 2013 at 3:14
  • Will there be multiple days in the array? Commented Aug 29, 2013 at 3:14
  • @Fred -ii-: it's a good practice to place it there Commented Aug 29, 2013 at 3:15
  • @zerkms I've seen it so many times where having a comma in a final entry, caused havoc. Now I'm really confused. Kind of a "darned if I don't and darned if I do". Commented Aug 29, 2013 at 3:16
  • its optional, like sarcasm :-) Commented Aug 29, 2013 at 3:16

3 Answers 3

1

One of the best ways to compare times is to convert your strings to times using strtotime().

// Original array of date strings
$dates = array(
  '08/29/2013 08:00',
  '08/29/2013 08:10',
  '08/29/2013 08:11',
  '08/29/2013 12:20',
  '08/29/2013 12:21',
  '08/29/2013 12:21',
  '08/29/2013 17:30',
);

// Make a new array of time stamps based on your strings
$dates_dt = array();
foreach($dates as $date)
  $dates_dt[] = strtotime($date);

// Same idea with the dates you want to look at
$date_last1 = strtotime('08/29/2013 08:00');
$date_last2 = strtotime('08/29/2013 13:00');
$date_first1 = strtotime('08/29/2013 12:00');
$date_first2 = strtotime('08/29/2013 17:00');

Here's the magic - some pretty simple functions that return the closest date before (or the first entry), and the closest after (or the last) in your array.

function dateBefore($date, $dateArray){
  $prev = $dateArray[0];
  foreach( $dateArray as $d ){
      if( $d >= $date )
          return date("Y-m-d H:i", $prev);
      $prev = $d;
  }
}

function dateAfter($date, $dateArray){
  foreach( $dateArray as $d ){
      if( $d > $date )
          return date("Y-m-d H:i", $d);
  }
  return date("Y-m-d H:i", end($dateArray));
}

echo dateBefore($date_last1, $dates_dt); // Outputs: 2013-08-29 08:00
echo dateBefore($date_last2, $dates_dt); // Outputs: 2013-08-29 12:21

echo dateAfter($date_first1, $dates_dt); // Outputs: 2013-08-29 12:20
echo dateAfter($date_first2, $dates_dt); // Outputs: 2013-08-29 17:30

http://codepad.org/cBYPuowt

Note Probably a good idea to sort the array of times as well so they're for sure in order.

foreach($dates as $date)
  $dates_dt[] = strtotime($date);

// Add sort here
sort($dates_dt);

http://codepad.org/jZPIEeJS

Sign up to request clarification or add additional context in comments.

Comments

0

You can convert all of the string timestamps to long values (milliseconds since 1970 epoch). Then just calculate the delta for relevant values (greater than 08/29/2013 08:00, for instance) compared to the upper limit (08/29/2013 13:00, for instance). The smallest delta is the closest to the upper limit. The greatest delta is furthest.

This is how you calculate the seconds since epoch: http://php.net/manual/en/function.strtotime.php

Comments

0
$nearest = $dates[0];
$now = time();
for ($i = 0; $i < count($dates); $i++) {
  $date = $dates[$i];
  $timestamp = strtotime($date);
  if (abs(strtotime($date) - $now) < abs(strtotime($nearest) - $now))
    $nearest = $date;
}
echo $nearest;

Comments

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.