How to Create Time Range Array in PHP

If you want to provide a time picker dropdown for selecting time, then there are many options to do that. You can use jQuery plugins to provide a time picker to the user or generate a custom time picker dropdown with PHP. If you are concern about the web page load time, then it will be better to use custom time range dropdown for selecting time. Using PHP you can easily create a time range array that helps you to generate time picker drop-down HTML. In this short tutorial, we’ll show you how to create time range in PHP and display a time picker dropdown using time range array.

For better usability we’ll place all code into a PHP function called create_time_range(). Using create_time_range() function you can easily get the range of times based on your required time duration. The create_time_range() function creates an array of time range with specified time interval. The create_time_range() function accepts four parameters and returns the time range array.

  • $start » Required. Specifies the start time. E.g., 7:30am or 7:30
  • $end » Required. Specifies the end time. E.g., 8:30pm or 20:30
  • $interval » Optional. Specifies the interval used in time range. E.g., 1 hour, 1 mins, 1 secs, etc
  • $format » Optional. Specifies which time format you want to get in the time range. E.g., 12 or 24
/** 
 * create time range by CodexWorld
 *  
 * @param mixed $start start time, e.g., 7:30am or 7:30 
 * @param mixed $end   end time, e.g., 8:30pm or 20:30 
 * @param string $interval time intervals, 1 hour, 1 mins, 1 secs, etc.
 * @param string $format time format, e.g., 12 or 24
 */ 
function create_time_range($start$end$interval '30 mins'$format '12') {
    
$startTime strtotime($start); 
    
$endTime   strtotime($end);
    
$returnTimeFormat = ($format == '12')?'g:i:s A':'G:i:s';

    
$current   time(); 
    
$addTime   strtotime('+'.$interval$current); 
    
$diff      $addTime $current;

    
$times = array(); 
    while (
$startTime $endTime) { 
        
$times[] = date($returnTimeFormat$startTime); 
        
$startTime += $diff
    } 
    
$times[] = date($returnTimeFormat$startTime); 
    return 
$times
}

Use the create_time_range() function like the below to get the time range array of specific time duration.

// create array of time ranges 
$times create_time_range('7:30''18:30''30 mins');

The above code returns the time range array like the below.

Array
(
    [0] => 7:30:00 AM
    [1] => 8:00:00 AM
    [2] => 8:30:00 AM
    [3] => 9:00:00 AM
    [4] => 9:30:00 AM
    [5] => 10:00:00 AM
    [6] => 10:30:00 AM
    [7] => 11:00:00 AM
    [8] => 11:30:00 AM
    [9] => 12:00:00 PM
    [10] => 12:30:00 PM
    [11] => 1:00:00 PM
    [12] => 1:30:00 PM
    [13] => 2:00:00 PM
    [14] => 2:30:00 PM
    [15] => 3:00:00 PM
    [16] => 3:30:00 PM
    [17] => 4:00:00 PM
    [18] => 4:30:00 PM
    [19] => 5:00:00 PM
    [20] => 5:30:00 PM
    [21] => 6:00:00 PM
    [22] => 6:30:00 PM
)

The following HTML and PHP code will generate a time picker dropdown using the time range array.

<select name="time_picker">
    <option value="">Select Time</option>
    <?php foreach($times as $key=>$val){ ?>
    <option value="<?php echo $val?>"><?php echo $val?></option>
    <?php ?>
</select>

Do you want to get implementation help, or enhance the functionality of this script? Click here to Submit Service Request

4 Comments

  1. Bharath Said...
  2. James Bond Said...
  3. Tejashri Said...
  4. Darwin Said...

Leave a reply

keyboard_double_arrow_up