如何使用PHP從開始時間和結束時間中取得一系列時間,每個時間之間相差一個小時?
P粉994092873
P粉994092873 2024-03-28 08:34:02
0
2
364

$course_hour = 1; $starttime = '07:00'; $endtime = '16:00';

我希望它是否可以傳回如下所示的值數組: 如果

$course_hour = 1;

$return_array = array('07:00 - 08:00', '08:00 - 09:00', '09:00 - 10:00', '10:00 - 11:00', '11 :00 - 12:00', '12:00 - 13:00', '13:00 - 14:00', '14:00 - 15:00', '15:00 - 16:00'); 如果

$course_hour = 1.5;

$return_array = array('07:00 - 08:30', '8:30 - 10:00', '10:00 - 11:30', '11:30 - 13:00', '13 :00 - 14:30', '14:30 - 16:00'); 如果

$course_hour = 2;

$return_array = array('07:00 - 09:00', '09:00 - 11:00', '11:00 - 13:00', '13:00 - 15:00');< /p>

function get_session_times($course_hour, $start_time, $end_time){
    if(strlen($start_time) < 5) $start_time = '0'.$start_time;
    if(strlen($end_time) < 5) $end_time = '0'.$end_time;
    $current_time = date("Y-m-d H:i");
    $session_starttime = date("Y-m-d H:i", strtotime(date("Y-m-d ").$start_time));
    $session_endtime = date("Y-m-d H:i", strtotime(date("Y-m-d ").$end_time));
    $session_starttime_ms = strtotime($session_starttime);
    $session_endtime_ms = strtotime($session_endtime);
    $session_times_array = array();
    for ($i = $session_starttime_ms; $i < $session_endtime_ms; ) { 
         $session_times_array[] = date("H:i", $i)."<br/>"; 
         $i = $i + ($course_hour * 60 * 60);
    }
    return $session_times_array;
}

P粉994092873
P粉994092873

全部回覆(2)
P粉609866533

雷雷

P粉294954447
/**
 * Get session times from start to end.
 * 
 * @link https://stackoverflow.com/a/27497314/128761 Convert decimal to hour and minute.
 * @link https://stackoverflow.com/a/62064286/128761 Original source code for get session times.
 * @param int|float|double|decimal $course_hour Course hour.
 * @param string $start_time Start time. Format is Hour:minute HH:MM.
 * @param string $end_time End time.
 * @return array Return times in array.
 */
function getSessionTimes($course_hour, $start_time, $end_time): array
{
    if (!is_numeric($course_hour)) {
        return [];
    }
    if (empty($start_time) || empty($end_time)) {
        return [];
    }

    $courseHour = (int) $course_hour;
    $courseMinute = (fmod($course_hour, 1) * 60);

    $beginDate = new DateTime(date('Y-m-d') . ' ' . $start_time);
    $endDate = new DateTime(date('Y-m-d') . ' ' . $end_time);

    $result = [];
    while ($beginDate < $endDate) {
        $output = $beginDate->format('H:i') . ' - ';
        $beginDate->modify('+' . $courseHour . 'hour ' . $courseMinute . 'minute');
        if ($beginDate > $endDate) {
            break;
        }
        $output .= $beginDate->format('H:i');
        $result[] = $output;
        unset($output);
    }

    unset($beginDate, $endDate);
    return $result;
}

您必須根據 1、1.5)轉換為小時和分鐘。 com/a/27497314/128761">這個答案

然後我使用mickmackusa提到的這個答案中的程式碼來取得時間數組。

這是測試。

/**
 * This function is for test the result only. It is no need in your real project.
 */
function testResult($result, $expect)
{
    if (!is_array($result) || !is_array($expect)) {
        throw new Exception('The result and expect must be array.');
    }
    
    $totalResult = count($result);
    $totalExpect = count($expect);
    $totalTested = ($totalResult === $totalExpect);
    unset($totalResult, $totalExpect);

    foreach ($result as $index => $eachResult) {
        if (isset($expect[$index]) && $expect[$index] === $eachResult) {
            // matched.
        } else {
            throw new Exception('The result and expect at index ' . $index . ' does not matched. (' . $eachResult . ')');
        }
    }
    
    return $totalTested;
}

// run tests
$result = getSessionTimes(1, '07:00', '16:00');
$expect = ['07:00 - 08:00', '08:00 - 09:00', '09:00 - 10:00', '10:00 - 11:00', '11:00 - 12:00', '12:00 - 13:00', '13:00 - 14:00', '14:00 - 15:00', '15:00 - 16:00'];
print_r($result);
var_dump(testResult($result, $expect));

$result = getSessionTimes(1.5, '07:00', '16:00');
$expect = ['07:00 - 08:30', '08:30 - 10:00', '10:00 - 11:30', '11:30 - 13:00', '13:00 - 14:30', '14:30 - 16:00'];
print_r($result);
var_dump(testResult($result, $expect));

$result = getSessionTimes(2, '07:00', '16:00');
$expect = ['07:00 - 09:00', '09:00 - 11:00', '11:00 - 13:00', '13:00 - 15:00'];
print_r($result);
var_dump(testResult($result, $expect));

結果將會是...

熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!