使用碳的开始和结束日期生成4次

时间:2018-11-04 20:21:55

标签: php laravel php-carbon

我正在为学校制作一种免费的体育博彩应用程序。人们可以预测即将到来的足球比赛的得分,并在正确的情况下获得积分奖励。

应用程序的下一步是确定何时选择获胜者。在整个赛季中,我需要挑选4名优胜者。我知道我的开始日期和结束日期。我想按以下顺序选择获奖者:

  • 第一名=竞赛的1/4
  • 第二名=竞赛的2/4
  • 第三名=竞赛的3/4
  • 第四名=比赛的4/4

我想在数据库中存储4个日期,并将这些日期添加为挑选获奖者的时间。 (而不是我想手动选择4次,所以我不必担心下个赛季什么时候选择新的获胜者)

注意:我不希望在可以进行比赛的时间内挑选优胜者。因此,从星期五到星期一23:59是不允许的。基本上是在比赛日结束时挑选赢家。

我当时计划使用Carbon,但是我对时间的计算并不熟悉。

代码

public function handle()
{
    $this->updateStatus();

    $startDate = '2018-08-10';
    $endDate   = '2019-05-12';

    // need 4 times between startDate and endDate
}

2 个答案:

答案 0 :(得分:1)

这个示例在开始日期和结束日期之间以固定间隔生成四个日期。

<?php

require "vendor/autoload.php";

use Carbon\Carbon;

$startDate = new Carbon('2018-08-10');
$endDate = new Carbon('2019-05-12');

$n = 4; // Number of dates

// difference (in days) between start date and end date divided by th number of dates 
// 5 make sure to generate dates in the range [startDate, endDate]
$diff = (int) $startDate->diffInDays($endDate) / $n - 5 ; 
$fourDate = [];
$newDate = clone $startDate;

for($i = 0; $i < $n; $i++) {

    $newDate->addDays($diff);

    switch ($newDate->dayOfWeek) {
        case 0:              //  0 (for Sunday)
            $newDate->addDays(2);
            break;
        case 1:             //  0 (for Monday)
            $newDate->addDays(1);
            break;
        case 5:             // 5 (for Friday)
            $newDate->addDays(4);
            break;
        case 6:             //  6 (for Saturday)
            $newDate->addDays(3);
            break;
        default:
            break;
    }
    $fourDate[] = $newDate->toDateString();
}

echo $startDate->toDateString() . PHP_EOL;
print_r($fourDate);
echo($endDate->toDateString());

输出

2018-08-10
Array
(
    [0] => 2018-10-16
    [1] => 2018-12-18
    [2] => 2019-02-19
    [3] => 2019-04-23
)
2019-05-12

没有固定间隔

<?php

require "vendor/autoload.php";

use Carbon\Carbon;

$startDate = new Carbon('2018-08-10');
$endDate = new Carbon('2019-05-12');

$n = 4; // Number of dates

// difference (in days) between start date and end date divided by th number of dates 
// 5 make sure to generate dates in the range [startDate, endDate]
$diff = (int) $startDate->diffInDays($endDate) / $n - 5 ; 
$fourDate = [];
$newDate = clone $startDate;

for($i = 0; $i < $n; $i++) {

    $newDiff = $diff - rand(1, $diff);

    $newDate->addDays($newDiff);

    switch ($newDate->dayOfWeek) {
        case 0:              //  0 (for Sunday)
            $newDate->addDays(2);
            break;
        case 1:             //  0 (for Monday)
            $newDate->addDays(1);
            break;
        case 5:             // 5 (for Friday)
            $newDate->addDays(4);
            break;
        case 6:             //  6 (for Saturday)
            $newDate->addDays(3);
            break;
        default:
            break;
    }
    $fourDate[] = $newDate->toDateString();
}

echo $startDate->toDateString() . PHP_EOL;
print_r($fourDate);
echo($endDate->toDateString());

输出

2018-08-10
Array
(
    [0] => 2018-09-18
    [1] => 2018-10-23
    [2] => 2018-11-06
    [3] => 2018-11-20
)
2019-05-12

答案 1 :(得分:1)

根据您的描述,这是我的看法,我希望它能使您对如何使用Carbon有所了解:

首先从给定的日期创建Carbon对象:

$start = Carbon::createFromFormat('Y-m-d', '2018-08-10');
$end = Carbon::createFromFormat('Y-m-d', '2019-05-12');

// then calculated the difference in days
// using floor to get an exact number of days instead of floating point number. 
// for example here is 275 / 4 = 68.75 so the result will be 68
$days = floor($start->diffInDays($end) / 4); 

// use copy, because Carbon is mutable, which means if you don't use copy it 
// will modify the result on the original date.
$firstQuarter = $start->copy()->addDays($days);
while($firstQuarter->isWeekend()) {
    $firstQuarter->addDay();
}

$secondQuarter = $firstQuarter->copy()->addDays($days);
// do the same check to see if it is weekend or the days that you don't want.. 
// and continue the chain the same for third and last day should be the fourth quarter.

别忘了从顶部导入Carbon

让我知道是否有帮助:)