views:

35

answers:

3

Hi, I loop through an array looking at the date (as key) and if it is before the event date I use the rating value (as value). Basically I'm looking for the closest value in the array before the event date.

My code:

$ratingData = $player->ratingData;
foreach ($ratingData as $ratingDate => $ratingValue) {
    list($year, $month, $day) = explode('-', $ratingDate);
    $ratingTimestamp = mktime(0, 0, 0, $month, $day, $year);
    if ($ratingTimestamp < $event->date) $ratingEvent = $ratingValue;
    else break;
}
die('<p>rating used = ' . $ratingValue . ' from ' . $ratingDate .
  ' for eventdate ' . date('Y-m-d', $event->date) . '</p><p>ratingtimestamp = '. 
  $ratingTimestamp . ' vs eventdate = ' . $event->date);

My output:

rating used = 2467.9650092431 from 2005-01-17 for eventdate 2005-01-17

ratingtimestamp = 1105912800 vs eventdate = 1105912800

Clearly that is not right (if ($ratingTimestamp < $event->date))? Why is the smaller than not working?

A: 

You shouldn't use break, because it makes the foreach stop. Try using else continue;. I think it should solve your issue (if I got it right)

Hope I have helped.

Regards from France ;)

Squ36
A: 

Sorry, it is correct.

I need to use $ratingEvent

sigh, pardon for any inconvenience.

Tjorriemorrie
Don't answer your own question. Edit the original text if you want to add additional information.
Anax
A: 

You exit loop when $ratingTimestamp >= $event->date, so it is possible to have them equal in your output. Although $ratingValue is correct, the time is from next event.

ghaxx