释放灯具的好方法

时间:2010-04-28 14:38:39

标签: php

嘿,我的问题如下, 我正在尝试创建一个代码,其中一组运动装置创建日期。 假设我有8支球队,有7轮比赛。

我已经生成了灯具,但想要在它们上添加日期生成。 因此,如果我有7轮,我会花28天,从现在开始的4天,从现在开始的8天,等等。

这样做的最佳方法是什么? 感谢

2 个答案:

答案 0 :(得分:0)

使用来自php-cli的strtotime()的示例:

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

答案 1 :(得分:0)

这应该做你想要的,并允许不均匀的团队数量。由于四舍五入,日期可能并不完美:

    $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);