Last active
August 29, 2015 14:23
-
-
Save LittleMikeNZ/3cac95e0c7a9a6be648d to your computer and use it in GitHub Desktop.
PHP class to create a range of times suitable for form select dropdowns.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Usage: | |
create array of time ranges | |
$times = create_time_range('9:30', '17:30', '30 mins'); | |
more examples: | |
$times = create_time_range('9:30am', '5:30pm', '30 mins'); | |
$times = create_time_range('9:30am', '5:30pm', '1 mins'); | |
$times = create_time_range('9:30am', '5:30pm', '30 secs'); | |
format the unix timestamps: | |
foreach ($times as $key => $time) { | |
$times[$key] = date('g:i:s', $time); | |
} | |
*/ | |
function create_time_range($start, $end, $step = '30 mins') { | |
$start_time = strtotime($start); | |
$end_time = strtotime($end); | |
$current = time(); | |
$add_time = strtotime('+' . $step, $current); | |
$diff = $add_time-$current; | |
$times = array(); | |
while ($start_time < $end_time) { | |
$times[] = $start_time; | |
$start_time += $diff; | |
} | |
$times[] = $start_time; | |
return $times; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment