Looping through with dates
Posted
by Luke
on Stack Overflow
See other posts from Stack Overflow
or by Luke
Published on 2010-06-01T19:13:15Z
Indexed on
2010/06/01
19:13 UTC
Read the original article
Hit count: 159
php
I have created a fixture generator for football/ soccer games...
$totalRounds = $teams - 1;
$matchesPerRound = $teams / 2;
$rounds = array();
$roundDates = array();
$curTime = time();
for ($i = 1; $i <= $totalRounds; $i++) {
$rounds[$i] = array();
$numDays = $i * 4;
$roundDates[$i] = strtotime("+".$numDays." days",$curTime);
}
foreach($roundDates as $time) {
for ($round = 0; $round < $totalRounds; $round++) {
for ($match = 0; $match < $matchesPerRound; $match++) {
$home = ($round + $match) % ($teams - 1);
$away = ($teams - 1 - $match + $round) % ($teams - 1);
// Last team stays in the same place while the others
// rotate around it.
if ($match == 0) {
$away = $teams - 1;
}
$rounds[$round][$match] = "$user[$home]~$team[$home]@$user[$away]~$team[$away]~$time";
}
}
}
In the code, for $i = 1, i thought that if you want the first date 4 days from now, the i must be 1, so 1 * 4 = 4. If i was 0, 0 * 4 equals 0. I assume this is correct thinking?
Anyway the main question is, trying to generate the dates isn't working. When i created a fixture list for 4 users home and away, I got 12 fixtures. 10 of these had the same date on them, and the other 2 didnt have a date.
Can anyone help me with this? Thanks
© Stack Overflow or respective owner