1

Possible Duplicate:
How to get most recent date from an array of dates?

I have a timetable which is an array:

array(
    '01.01.2012|11:00',
    '01.01.2012|14:30',
    '01.01.2012|16:24', // example match
    '01.01.2012|17:20',
    '01.01.2012|17:43',
    '02.01.2012|10:20',
    '02.01.2012|12:30',
); // etc.

I want a Cron job that will check current date/time and compare to ones in array. First I check if the date is a match, that's no problem. But then I need to find and display the earliest time from that array which is after the current time. If there's no suitable time within the same date then I display the earliest time from the next date.

For example: lets take the array above and current date/time of 01.01.2012|14:45

  • The date is a match so we continue
  • The next time from the array 16:24, but how to find it using PHP? And if the current time is higher than anything for the same date in array, then get the earliest time from next date?

Obviously to get the string with correct date I use "foreach" and "if" and it returns fine. But then how do I go through the times?

Community
  • 1
  • 1
Pocker Player
  • 31
  • 2
  • 8

4 Answers4

2

Convert to timestamp, sort and iteratively compare with current time.

$ts = array_map(
        create_function('$a','return strtotime(str_replace("|", " ", $a));'), 
        $dates);
$len= count($ts); $now = time();
sort($ts); 
for($i=0;$i<$len && (!($now<$ts[$i]));$i++);
echo date("d.m.Y|H:i",$ts[$i]);

Functions of Interest

Shiplu Mokaddim
  • 56,364
  • 17
  • 141
  • 187
0

If you convert these to UNIX timestamps, you can sort them numerically, loop through and take the first item that is larger than the current timestamp.

jokkedk
  • 2,390
  • 2
  • 17
  • 21
0

You might consider converting those dates to UNIX timestamps:

function getUnixTimestamp($string) {
    list($date, $time) = explode('|', $string);
    return strtotime("$date $time");
}

Then you could use something like:

$array = array(); // from example
$timestamps = array_map('getUnixTimestamp', $array);
$current = time();

// create an array mapping timestamp to string
$keyedArray = array_combine($timestamps, $array);

// sort by timestamp
ksort($keyedArray);

foreach ($keyedArray as $timestamp => $string) {
    if ($timestamp > $current) {
        // this is the first timestamp after current time
    }
}

You may want to do some extra checking on $timestamp, making sure it's either on the same or next day, but it's easier to work with timestmap comparisons than string matches.

cmbuckley
  • 40,217
  • 9
  • 77
  • 91
0

Write a comparison function for the date-time format you have then loop through until you find a date greater or equal to your reference date.

function compareDateTime($dt1, $dt2) {
   sscanf($dt1, "%d.%d.%d|%d:%d", $day, $month, $year, $hour, $minute);
   $comp1 = $year . $month . $day . $hour . $minute;
   sscanf($dt1, "%d.%d.%d|%d:%d", $day, $month, $year, $hour, $minute);
   $comp2 = $year . $month . $day . $hour . $minute;

   return $comp1 - $comp2;
}

Returns -ve when $dt1 < $dt2 and +ve when $dt1 > $dt2

SpacedMonkey
  • 2,725
  • 1
  • 16
  • 17