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
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>'            
        }
    }
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.