List to show time - half hour interval

Discussion in 'PHP' started by newlearn, Apr 13, 2007.

  1. #1
    I have a PHP page to show a select list of time in half an hour interval.

    For now I selected it manually
    <option value="oneThirty">1:30AM</option>
    <option value="two">2AM</option>
    ...

    Is there way I can do it using loop in PHP with half hour interval and how?

    Thanks
     
    newlearn, Apr 13, 2007 IP
  2. lbalance

    lbalance Peon

    Messages:
    381
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    0
    #2
    maybe something like this:

    <?
     for($hour=0;$hour<24;$hour++){
      if($hour<12){ $ampm = "AM"; } else { $ampm = "PM"; }
      $selTime .= "<option value=\"".sprintf("%02d",$hour).":00$ampm\">".sprintf("%02d",$hour).":00$ampm</option>";
      $selTime .= "<option value=\"".sprintf("%02d",$hour).":30$ampm\">".sprintf("%02d",$hour).":30$ampm</option>";
     }
    ?>
    
    <select>
     <?=$selTime?>
    </select>
    PHP:
     
    lbalance, Apr 13, 2007 IP
  3. newlearn

    newlearn Peon

    Messages:
    14
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks, that is helpful.
     
    newlearn, Apr 14, 2007 IP