tags:

views:

44

answers:

2

Hey, my problem is as follows, I am trying to create code where a set of sporting fixtures are created with dates on. Say I have 8 teams, with 7 rounds of fixtures.

I have generated the fixtures, but want to add a date generation on them. So if i had 7 rounds, I would put 28 days and it would make each round 4 days from now, 8 days from now, etc.

What would be the best way to go about doing this? Thanks

A: 

Example using strtotime() from php-cli:

php > echo date("Y-m-d", strtotime("+4 days"));
2010-05-02

php > echo date("Y-m-d", strtotime("+8 days"));
2010-05-06

php > echo date("Y-m-d", strtotime("+12 days"));
2010-05-10
simplemotives
Ahh i see, so if i make an algorithm that loops through adding 4 days onto every set of fixtures, it will work?
Luke
I would recommend testing it out for yourself, since you didn't include any code. We could comment further if you edited your question with the relevant code.
simplemotives
A: 

This should do what you want and allows for an uneven number of teams. Dates might not be perfect because of the rounding down:

    $teams = array("TEAM A","TEAM B","TEAM C","TEAM D","TEAM E", "TEAM F","TEAM G","TEAM H","TEAM I");
    $days = 28;
    $rounds = count($teams) -1;

    //Number of Days Between Fixtures
    $daysBetweenFixtures = floor($days / $rounds);


    $fixtures = array();

    for($i =0; $i < count($teams); $i++) {
        //Calculate Date of this round of fixtures
        $date  = date("D d M Y",mktime(0, 0, 0, date("m")  , date("d")+ ($i * $daysBetweenFixtures) , date("Y")));

        $hasFixtureToday = array();

        for($j=$i; $j<$i+count($teams);  $j=$j+2) {
            $homeTeam = $teams[$j % count($teams)];         
            $awayTeam = $teams[($j+1) % count($teams)];

            if(!in_array($homeTeam,$hasFixtureToday) && !in_array($awayTeam,$hasFixtureToday)) {
                $fixtures[$date][] = "{$homeTeam} vs {$awayTeam}";
                $hasFixtureToday[] = $homeTeam;
                $hasFixtureToday[] = $awayTeam;
            }
        }
    }
    print_r($fixtures);
jkilbride