Jump to content

Best Way to Format Times


hoopplaya4

Recommended Posts

Hi All,

 

I'm creating a calendar and when a user adds an event, they have the ability to select (from a <select> box) a start time and select from another box an end time. 

 

I have one quick question:

 

1) What is the easiest way to code in the values in fifteen minute incrementals?  In other words, instead of doing the following:

 

<select>
<option value="12am">12:00 AM</option>
<option value="12:15am">12:15 AM</option>
<option value="12:30am">12:30 AM</option>
<option value="12:45am">12:45 AM</option>

... so on

 

How could I use PHP to take up less space?

 

 

Thanks!

Link to comment
https://forums.phpfreaks.com/topic/160204-best-way-to-format-times/
Share on other sites

Use hours an minutes:

 

$hours = range(1,11);
$hours = array_unshift($times,12);
$timeOfDay = array('am','pm'):
$minutes = array(15,30,45);

foreach($timeOfDay =>$amOrPm){
    foreach($hours as $hour){
        echo '<option value="' . $hour . $amOrPm .'"> ' . $hour . ':00' . $amOrPm .' </option>'
        foreach($minutes as $minute){
            echo '<option value="' . $hour . ':'. $minute . $amOrPm .'"> ' . $hour . ':'. $minute . $amOrPm .' </option>'            
        }
    }
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.