0

I have a PHP page with a Due Date and Time selection option. What I would like to be able to do is limit the times in the time selection drop down to only show available working hours rather than a 24 hour time list from 00:00 to 24:00. Here is the code as it currently appears, but this is over my head and beyond my basic editing abilities. Any help or suggestions would be greatly appreciate. Thanks in advance,

<tr>
    <td align="left" valign="top">Due Date:</td>
    <td>
        <i>Time is based on your time zone (GM <?=$thisuser->getTZoffset()?>)</i>&nbsp;<font class="error">&nbsp;<?=$errors['time']?></font><br>
        <input id="duedate" name="duedate" value="<?=Format::htmlchars($info['duedate'])?>"
            onclick="event.cancelBubble=true;calendar(this);" autocomplete=OFF>
        <a href="#" onclick="event.cancelBubble=true;calendar(getObj('duedate')); return false;"><img src='images/cal.png'border=0 alt=""></a>
        &nbsp;&nbsp;
        <?php
         $min=$hr=null;
         if($info['time'])
            list($hr,$min)=explode(':',$info['time']);
            echo Misc::timeDropdown($hr,$min,'time');
        ?>
        &nbsp;<font class="error">&nbsp;<?=$errors['duedate']?></font>
    </td>
</tr>

1 Answers1

0

try with this code:

Best code:

function timeRange($start, $end, $interval="30 mins" , $outputFormat = null )
{
    $range = array();
    $start = strtotime($start); 
    $end   = strtotime($end);

    $current    = time();
    $interval   = strtotime('+'.$interval, $current);
    $diff       = $interval-$current;


    while ($start <= $end)
    {
        if( is_null($outputFormat) )
        {
            $range[] = $start;
        }else{
            $range[] = date( $outputFormat , $start);
        }
        $start += $diff;
    }
    return $range;
}
$times = timeRange('8:30', '17:30', '30 mins' , "H:i");

Output:

Array
(
    [0] => 08:30
    [1] => 09:00
    [2] => 09:30
    [3] => 10:00
    [4] => 10:30
    [5] => 11:00
    [6] => 11:30
    [7] => 12:00
    [8] => 12:30
    [9] => 13:00
    [10] => 13:30
    [11] => 14:00
    [12] => 14:30
    [13] => 15:00
    [14] => 15:30
    [15] => 16:00
    [16] => 16:30
    [17] => 17:00
    [18] => 17:30
)

Simple code:

function timeInterval( $start , $end , array $interval = array() )
{
    $list = array();
    for( $i = $start; $i <= $end ;$i++ )
    {
        foreach( $interval AS $min )
        {
            $list[] = sprintf("%02d",$i). ":" . $min;
        }
    }
    return $list;   
}


highlight_string( print_r( timeInterval( 8 , 17 , array( "00" , "30" ) ), true ) );

Output:

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

And your HTML:

<tr>
    <td align="left" valign="top">Due Date:</td>
    <td>
        <i>Time is based on your time zone (GM <?php echo $thisuser->getTZoffset();?>)</i>
        &nbsp;
        <font class="error">
            &nbsp;<?php echo $errors["time"];?>
        </font>
        <br/>
        <input id="duedate" name="duedate" value="<?php echo Format::htmlchars($info["duedate"]);?>" onclick="event.cancelBubble=true;calendar(this);" autocomplete="OFF" />
        <a href="#" onclick="event.cancelBubble=true;calendar(getObj('duedate')); return false;">
            <img src='images/cal.png'border=0 alt="">
        </a>
        &nbsp;&nbsp;
        <select name="time">
            <option value="">Select you time...</option>
            <?php
            foreach( timeRange('9:30', '17:30', '30 mins' , "H:i") AS $time )
            {
                echo "<option value=\"$time\">$time</option>";
            }
            ?>
        </select>
        &nbsp;<font class="error">&nbsp;<?php echo $errors["duedate"];?></font>
    </td>
</tr>

Bye!

Olaf Erlandsen
  • 5,817
  • 9
  • 41
  • 73
  • 1
    I will take a look at it when I get a chance and let you know how if it works. Thanks for taking the time to look at it for me. – Bryan Miller Oct 18 '13 at 19:25