2010-10-11 120 views

回答

9

感謝您重新打開問題alex。

這是一個應該與功能程序員產生共鳴的解決方案。

function halfHourTimes() { 
    $formatter = function ($time) { 
    if ($time % 3600 == 0) { 
     return date('ga', $time); 
    } else { 
     return date('g:ia', $time); 
    } 
    }; 
    $halfHourSteps = range(0, 47*1800, 1800); 
    return array_map($formatter, $halfHourSteps); 
} 
+0

不錯的解決方案!我認爲它需要PHP 5.3的函數分配給一個變量? +1(不要擔心重新開放,如果我知道你在努力解決問題,我永遠不會刪除它)。 – alex 2010-10-11 23:22:31

7

我決定這一次是更好:)

function hoursRange($lower = 0, $upper = 23, $step = 1, $format = NULL) { 

    if ($format === NULL) { 
     $format = 'g:ia'; // 9:30pm 
    } 
    $times = array(); 
    foreach(range($lower, $upper, $step) as $increment) { 
     $increment = number_format($increment, 2); 
     list($hour, $minutes) = explode('.', $increment); 
     $date = new DateTime($hour . ':' . $minutes * .6); 
     $times[(string) $increment] = $date->format($format); 
    } 
    return $times; 
} 
0

這也許是一個更優雅的方式,但它需要的時間是在幾秒鐘內(也使得它更靈活)。

function time_range($start, $end, $step = 1800) { 
    $return = array(); 
    for($time = $start; $time <= $end; $time += $step) 
     $return[] = date('g:ia', $time); 
    return $return; 
} 
18

下面是一個使用秒更精確的Alex's function的改進版本:

function hoursRange($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[(string) $increment] = $date->format($format); 
    } 

    return $times; 
} 

因此,爲了用1小時的間隔時間的陣列在24小時時間段期間,使用默認值:

hoursRange(); 

,這將給你如下:

Array 
(
    [00:00] => 12:00 am 
    [01:00] => 1:00 am 
    [02:00] => 2:00 am 
    [03:00] => 3:00 am 
    [04:00] => 4:00 am 
    [05:00] => 5:00 am 
    [06:00] => 6:00 am 
    [07:00] => 7:00 am 
    [08:00] => 8:00 am 
    [09:00] => 9:00 am 
    [10:00] => 10:00 am 
    [11:00] => 11:00 am 
    [12:00] => 12:00 pm 
    [13:00] => 1:00 pm 
    [14:00] => 2:00 pm 
    [15:00] => 3:00 pm 
    [16:00] => 4:00 pm 
    [17:00] => 5:00 pm 
    [18:00] => 6:00 pm 
    [19:00] => 7:00 pm 
    [20:00] => 8:00 pm 
    [21:00] => 9:00 pm 
    [22:00] => 10:00 pm 
    [23:00] => 11:00 pm 
) 

這裏有幾個例子使用:

// Every 15 Minutes, All Day Long 
$range = hoursRange(0, 86400, 60 * 15); 

// Every 30 Minutes from 8 AM - 5 PM, using Custom Time Format 
$range = hoursRange(28800, 61200, 60 * 30, 'h:i a'); 

您可以查看working snippet at CodePad

+0

看起來不錯 - 只是不確定爲什麼要爆炸'$ increment'而不是直接將它傳遞到DateTime構造函數中? – 2015-10-21 17:26:56

1

簡單的解決方案

$h = 0; 
while ($h < 24) { 
    $key = date('H:i', strtotime(date('Y-m-d') . ' + ' . $h . ' hours')); 
    $value = date('h:i A', strtotime(date('Y-m-d') . ' + ' . $h . ' hours')); 
    $formatter[$key] = $value; 
    $h++; 
} 

結果是:

Array 
(
    [00:00] => 12:00 AM 
    [01:00] => 01:00 AM 
    [02:00] => 02:00 AM 
    [03:00] => 03:00 AM 
    [04:00] => 04:00 AM 
    [05:00] => 05:00 AM 
    [06:00] => 06:00 AM 
    [07:00] => 07:00 AM 
    [08:00] => 08:00 AM 
    [09:00] => 09:00 AM 
    [10:00] => 10:00 AM 
    [11:00] => 11:00 AM 
    [12:00] => 12:00 PM 
    [13:00] => 01:00 PM 
    [14:00] => 02:00 PM 
    [15:00] => 03:00 PM 
    [16:00] => 04:00 PM 
    [17:00] => 05:00 PM 
    [18:00] => 06:00 PM 
    [19:00] => 07:00 PM 
    [20:00] => 08:00 PM 
    [21:00] => 09:00 PM 
    [22:00] => 10:00 PM 
    [23:00] => 11:00 PM 
) 
0

這裏有一個更靈活的版本,不需要日期時間(因爲我們已經與時間戳以秒爲工作反正)。 ;-)

function get_hours_range($start = 0, $end = 86400, $step = 3600, $format = 'g:i a') { 
     $times = array(); 
     foreach (range($start, $end, $step) as $timestamp) { 
       $hour_mins = gmdate('H:i', $timestamp); 
       if (! empty($format)) 
         $times[$hour_mins] = gmdate($format, $timestamp); 
       else $times[$hour_mins] = $hour_mins; 
     } 
     return $times; 
} 
0

這裏是我的建議:

$start = new \DateTime('00:00'); 
    $times = 24 * 2; // 24 hours * 30 mins in an hour 

    for ($i = 0; $i < $times-1; $i++) { 
     $result[] = $start->add(new \DateInterval('PT30M'))->format('H:i A'); 
    } 

    print_r($result); 

希望這有助於。

相關問題