根据上班时间点 切割分成时间段

/**
 * 根据班次 获取时间段 09:00 - 18:00
 * start 09:00,
 * end 18:00
 * duration 时长 min
 * freeTime 每时段间隔多少分钟
 * 
*/
function getChunkTimeList($start, $end, int $duration= 45, int $freeTime = 15):array
{

    $duration *=60;
    $freeTime *=60;
    $day = date('Y-m-d');

    $startTime = strtotime($day.$start);
    $endTime = strtotime($day.$end);

    $list = [];

    $sum = $startTime;

    for ($startTime; $startTime<= $endTime; $startTime += $duration) {

        $temp = $sum+$duration;

        if ($temp> $endTime) {
            break;
        }

        $list[] = ['start_time' => date('H:i', $sum), 'end_time' => date('H:i', $temp)];

        $sum = $temp + $freeTime;
    }

    return $list;
}




$list = getChunkTimeList('9:00', '18:00');

=====================================
array(9) {
[0]=>
array(2) {
[“start_time”]=>
string(5) “09:00”
[“end_time”]=>
string(5) “09:45”
}
[1]=>
array(2) {
[“start_time”]=>
string(5) “10:00”
[“end_time”]=>
string(5) “10:45”
}
[2]=>
array(2) {
[“start_time”]=>
string(5) “11:00”
[“end_time”]=>
string(5) “11:45”
}
[3]=>
array(2) {
[“start_time”]=>
string(5) “12:00”
[“end_time”]=>
string(5) “12:45”
}
[4]=>
array(2) {
[“start_time”]=>
string(5) “13:00”
[“end_time”]=>
string(5) “13:45”
}
[5]=>
array(2) {
[“start_time”]=>
string(5) “14:00”
[“end_time”]=>
string(5) “14:45”
}
[6]=>
array(2) {
[“start_time”]=>
string(5) “15:00”
[“end_time”]=>
string(5) “15:45”
}
[7]=>
array(2) {
[“start_time”]=>
string(5) “16:00”
[“end_time”]=>
string(5) “16:45”
}
[8]=>
array(2) {
[“start_time”]=>
string(5) “17:00”
[“end_time”]=>
string(5) “17:45”
}
}

本作品采用《CC 协议》,转载必须注明作者和本文链接
讨论数量: 1

好像这样短一些

/**
 * 根据班次 获取时间段 09:00 - 18:00
 * start 09:00,
 * end 18:00
 * duration 时长 min
 * freeTime 每时段间隔多少分钟
 *
 */
function getChunkTimeList($start, $end, int $duration = 45, int $freeTime = 15): array
{
    $duration *= 60;
    $freeTime *= 60;

    $arr = range(strtotime($start), strtotime($end) - $duration, $duration + $freeTime);

    return array_map(fn($i) => [
        'start_time' => date('H:i', $i),
        'end_time' => date('H:i', $i + $duration),
    ], $arr);
}

$list = getChunkTimeList('9:00', '18:00');
1个月前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!