简体   繁体   English

PHP在给定工作日范围内选择一个随机日期

[英]PHP Select a random date in a range from given weekday

I have the following function that Generateds a random date: 我有以下生成随机日期的函数:

   function createARandomDateBetweenRange(Carbon $start, Carbon $stop, int $weekDay=null) : Carbon
    {
        $randDate = random_int($start->getTimestamp(), $stop->getTimestamp());
        $radomDate = Carbon::createFromTimestamp($randDate);

        return $radomDate
    }

But for some reason I need to be able to select a random Monday, tuesday etc etc from a provided weekday (0 is Sunday, 6 is Saturday) 但是由于某种原因,我需要能够从提供的工作日中随机选择星期一,星期二等(0是星期日,6是星期六)

So I changed the definition as: 所以我将定义更改为:

use Carbon\Carbon;

    public static function createARandomDateBetweenRange(Carbon $start, Carbon $stop, ?int $weekDay=null) : Carbon
    {
        $randDate = random_int($start->getTimestamp(), $stop->getTimestamp());
        $radomDate = Carbon::createFromTimestamp($randDate);

        if($weekDay!=null && $weeekDay >= Carbon::SUNDAY  && $weekDay <= Carbon::SATURDAY)
        {
          // Modify the date in order to get the correct date here

        }

        return $radomDate;
    }

So how I can get the correct date from a specified range of dated and that random datetime to be a specific weekday I provide as a parameter? 那么,如何从指定的日期范围中获取正确的日期,以及如何将随机日期时间作为参数提供的特定工作日呢?

For example when I call the function like: 例如,当我调用类似的函数时:

use Carbon\Carbon;

$now = Carbon::now()
$fiveMonthsLater= new Carbon($now);
$fiveMonthsLater->modify("+5 months");

$randomTuesday=createARandomDateBetweenRange($now,$fiveMonthsLater,Carbon::TUESDAY);

$randomSaturday=createARandomDateBetweenRange($now,$fiveMonthsLater,Carbon::SATURDAY);

echo $randomTuesday->isoFormat("dddd"); //Outputs Tuesday
echo $randomSaturday->isoFormat("dddd"); //Outputs Saturdat

You could filter the period to the day you want, then randomly choose one. 您可以将时段过滤到所需的日期,然后随机选择一个。

use Carbon\CarbonPeriod;

$period = CarbonPeriod::between('2000-01-01', '2000-12-31');

// Define your filters
$mondayFilter = function ($date) { return $date->isMonday(); };
$tuesdayFilter = function ($date) { return $date->isTuesday(); };
...

// Apply your filter
$mondays = $period->filter($mondayFilter)->toArray();

// Grab a random date from your filtered period. 
var_dump(
    $mondays[array_rand($mondays)]
);

EDIT 编辑

Integrating it into your current example: 将其集成到您的当前示例中:

use Carbon\CarbonPeriod;

function createARandomDateBetweenRange($start, $end, $filter = null)
{
    $period = CarbonPeriod::between($start, $end);

    if ($filter) {
        $period = $period->filter($filter);
    }

    return $period;
}

// Define your filters
$mondayFilter = function ($date) { return $date->isMonday(); };
$tuesdayFilter = function ($date) { return $date->isTuesday(); };

// Optionally, pass in your filter.
$period = createARandomDateBetweenRange('2001-01-01', '2001-12-31', $tuesdayFilter);

$period = $period->toArray();

var_dump(
    $period[array_rand($period)]
);

Maybe instead of all that Carbon you could try this? 也许可以尝试所有碳,而不是碳? I have just written this on-the-fly but it appears to work well. 我刚刚写了这篇文章,但看起来效果很好。 Hopefully it will give you some ideas. 希望它会给您一些想法。

Basically, it finds the first matching day in the range, then works out how many full weeks fit from that point 'til the end and uses that number for the random part. 基本上,它会找到该范围内的第一个匹配日,然后算出从该点到结束的整整几周,并将该数字用于随机部分。

// The Function
function nextRandomDay($dayLabel,$from,$to){
    $i=0;
    while(date("l",$from) != $dayLabel && $i<10){
        $i++;
        $from = strtotime("NOW + 1 day",$from);
    }
    if($i>8){return false;} // Invalid day label
    return strtotime("NOW + ".rand(0,floor(($to-$from) / (86400*7)))." weeks",$from);
}

// EXAMPLE
$from   = time(); // Where to start looking from
$to     = strtotime("NOW + 71 days"); // When to stop looking
$day    = "Tuesday"; // Full string day name

if($myDate = nextRandomDay($day,$from,$to)){
    echo "Found a '$day' on ".date("d/m/Y",$myDate)." ($myDate)";
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM