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

Php 使用含碳的开始和结束日期生成4次,php,laravel,php-carbon,Php,Laravel,Php Carbon,我正在为我的学校申请一种免费的体育博彩。人们可以预测即将到来的足球比赛的分数,如果分数是正确的,就可以得到奖励 我的应用程序的下一步是确定何时挑选优胜者。我需要在整个赛季中选出4名优胜者。我知道我的赛季开始日期和结束日期。我想按以下顺序挑选获奖者: 第一名胜利者=比赛的四分之一 第二名胜利者=比赛的四分之二 第三名胜利者=比赛的四分之三 第四名胜利者=比赛的四分之四 我想在我的数据库中存储4个日期,并将它们添加为挑选获奖者的时间。(我想这样做,而不是手动挑选4次,这样我就不必担心下赛季我将在

我正在为我的学校申请一种免费的体育博彩。人们可以预测即将到来的足球比赛的分数,如果分数是正确的,就可以得到奖励

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

  • 第一名胜利者=比赛的四分之一
  • 第二名胜利者=比赛的四分之二
  • 第三名胜利者=比赛的四分之三
  • 第四名胜利者=比赛的四分之四
我想在我的数据库中存储4个日期,并将它们添加为挑选获奖者的时间。(我想这样做,而不是手动挑选4次,这样我就不必担心下赛季我将在什么时候挑选新的获奖者)

注意:我不希望在比赛进行期间挑选赢家。所以从周五到周一23:59是不允许的。基本上是在比赛日结束时选择赢家。

我计划使用碳,但我不太熟悉时间的计算

代码

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());
没有固定的间隔

<?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());

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

<?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());
没有固定的间隔

<?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());

以下是我根据您的描述对此的看法,我希望它能让您了解如何使用碳:

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

$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.
别忘了在顶部输入碳


如果有帮助,请告诉我:)

以下是我根据您的描述对此的看法,我希望它能让您了解如何使用碳:

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

$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.
别忘了在顶部输入碳

如果有帮助,请告诉我:)