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

时间:2019-07-15 14:32:46

标签: php date

我具有以下生成随机日期的函数:

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

        return $radomDate
    }

但是由于某些原因,我需要能够从提供的工作日(0是星期日,6是星期六)中随机选择星期一,星期二等。

所以我将定义更改为:

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;
    }

那么如何从指定的日期范围中获取正确的日期,以及如何将随机日期时间作为我提供的特定工作日作为参数?

例如,当我调用类似的函数时:

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

2 个答案:

答案 0 :(得分:1)

您可以将时段过滤为所需的日期,然后随机选择一个。

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

编辑

将其集成到当前示例中:

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

答案 1 :(得分:0)

也许可以尝试所有这些功能而不是Carbon? 我刚刚写了这篇文章,但看起来效果很好。希望它将给您一些想法。

基本上,它会找到该范围内的第一个匹配日,然后算出从该点到结束的整整几周,并将该数字用于随机部分。

// 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)";
}