laravel 如何在数组中创建时间范围步长,如以下结果- PHP

8hhllhi2  于 2023-01-10  发布在  PHP
关注(0)|答案(4)|浏览(125)

我需要像阵列的例子,如果可以设置选项的一步,开始,结束是非常好的

[
        [
            "07:30:00",
            "08:00:00",
        ],
        [
            "08:00:00",
            "08:30:00",
        ],
            ....
        [
            "23:30:00",
            "00:00:00",
        ],
    ]
uqzxnwby

uqzxnwby1#

我想最好的解决办法是:

function timeSteps($step, $start, $end){
    $stepHours = substr($step, 0, 2);
    $stepMinutes = substr($step, 3, 2);
    $stepSeconds = substr($step, 6, 2);

    $startTime = Carbon::createFromFormat('H:i:s', $start);
    $endTime = Carbon::createFromFormat('H:i:s', $end);

    $result = [];

    while ($startTime->lt($endTime)) {
        $item = [];
        array_push($item, $startTime->format('H:i:s'));
        
        $startTime->addHours($stepHours);
        $startTime->addMinutes($stepMinutes);
        $startTime->addSeconds($stepSeconds);
        
        array_push($item, $startTime->format('H:i:s'));
        
        array_push($result, $item);
    }

    return $result;
}

你可以这样称呼它:

timeSteps("00:30:00", "08:00:00", "10:00:00")
nc1teljy

nc1teljy2#

如果你正在使用Laravel,那么你最好使用 CarbonInterval,正如这篇博客文章所述(作者:Jan Östlund):https://janostlund.com/2019-11-03/generate-time-ranges

$intervals = CarbonInterval::minutes($timeStep)->toPeriod($start, $end);

然后你会得到一个碳对象的数组,你可以使用你想要的:

foreach ($intervals as $date) {
    dump($date->format('H:i'))
}
xam8gpfp

xam8gpfp3#

我在laravel这样做,结果还可以

function range($lower = 0, $upper = 86400, $step = 3600, $format = '')
{
    $times = array();

    if ( empty( $format ) ) {
        $format = 'g:i a';
    }

    foreach ( range( $lower, $upper, $step ) as $increment ) {

        $increment = gmdate( 'H:i', $increment );

        list( $hour, $minutes ) = explode( ':', $increment );

        $date = new \DateTime( $hour . ':' . $minutes );

        $times[] = $date->format( $format );
    }

    $times =  collect($times)->chunk(2)->map(function($item) {
        return $item->values();
    });

    $new_times = [];
    foreach ($times as $key=>$time){
        if($key == 0){
            $new_times[$key][0] = $time[0];
            $new_times[$key][1] = $time[1];
        }
        else{
            $new_times[$key][0] = Carbon::parse($time[0])->subMinutes(30*$key)->format('H:i:s');
            $new_times[$key][1] = Carbon::parse($time[1])->subMinutes(30*$key)->format('H:i:s');
        }
    }
    return $new_times;
}

并使用:

range(27000, 144000, 1800, 'H:i:s');
qnzebej0

qnzebej04#

这里有几种可能性,但这种方法会保持简单...创建一个函数来创建子数组的开始和结束时间。然后,创建一个开始和结束时间的范围,例如:上午8:00到午夜。每增加1800秒(半小时),你就会得到你的结果。

<?php
function timeRange( $start = 0, $end = 86400, $step = 3600, $format = 'Y-m-d H:i:s' ) {
        $times = [];
        foreach ( range( $start, $end, $step ) as $inc ) {
                $inc = gmdate( 'H:i', $inc );
                $date = new DateTime( $inc );
                $times[(string) $inc] = $date->format( $format );
        }
        return $times;
}

$my_times = [];
$range = range(28800, 84600, 1800);

foreach($range as $seconds)
{
        $start = $seconds;
        $end = $seconds+1800;
        $my_times[] = timeRange($seconds, $end, 1800, 'H:i:s');
}

var_dump($my_times);

相关问题