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

Generating 4 times using a start and end date with carbon

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

我申请的下一步是确定何时挑选获胜者。我需要在整个赛季中选出 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
}

这是一个在开始日期和结束日期之间定期生成四个日期的示例。

<?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

以下是我根据您的描述得出的结论,希望它能让您对如何使用 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

如果有帮助请告诉我:)