Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Drop down time selector - is there an "elegant" way to do it in php

Posted on 2009-07-10
20
Medium Priority
?
272 Views
Last Modified: 2012-05-07
Hello

I need to add a dropdown field for times of the day, similar to the one on this page:

http://www.avis.com/car-rental/avisHome/home.ac

I can create one the usual way with html.  But I am wondering if there is a "better" way to do it in php.

The times would be in this format (every half hour)  8:00 AM, 8:30:AM  9:00 AM   and at 12  it would be 12:00 PM, 12:30 PM.

Of course I can do it the usual html way, but I wouldn't mind finding out if there was a '"php way" :)

Thanks

....Rowby
0
Comment
Question by:Rowby Goren
  • 9
  • 5
  • 3
  • +1
20 Comments
 
LVL 39

Accepted Solution

by:
Roger Baklund earned 1700 total points
ID: 24823727
The "php way" would be to use a loop. Try this:
echo '<select name="time">';
for($i=0;$i<24*60;$i+=30) {
  $time = date('h:i A',mktime($i/60,$i%60,0));
  echo '<option value="'.$time.'">'.$time.'</option>'; 
}
echo '</select>';

Open in new window

0
 
LVL 9

Assisted Solution

by:xBellox
xBellox earned 300 total points
ID: 24823770
Here is it, simply and clean:

<select name="hours">
<?	
    $time = strtotime("00:00:00");	
    for ($i=00;$i<1440;$i+=30) {
		$display = date("h:i A", strtotime("+ $i minutes", $time));
		echo "<option value='$display'>$display</option>";
    }
?>
</select>

Open in new window

0
 
LVL 4

Expert Comment

by:khsater
ID: 24823787
Attached is a relatively simple way to do it, but it could probably be shortened by someone who knows more functions than I do.  I can already think of a quicker way using a 24-hour system and checking if the number is more than 12, but it's not going to be much faster.
print '<option value="12:00a">12:00 A.M.</option>';
for($i = 1; $i<12 $i++){
   print '<option value="'.$i.':00a">'.$i.':00 A.M.</option>';
   print '<option value="'.$i.':30a">'.$i.':30 A.M.</option>';
}
print '<option value="12:00p">12:00 P.M.</option>';
for($i = 1; $i<12 $i++){
   print '<option value="'.$i.':00p">'.$i.':00 P.M.</option>';
   print '<option value="'.$i.':30p">'.$i.':30 P.M.</option>';
}

Open in new window

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 4

Expert Comment

by:khsater
ID: 24823795
There we go. xBellox's method is much nicer than mine.
0
 
LVL 9

Author Comment

by:Rowby Goren
ID: 24823978
Thanks all.

I am going to have "two" time drop downs in the forms.

Do I have to give each one an unique name or unique identifiers.

For example

Drop off your vehicle time:

Pick up your vehicle time:

Thanks

Rowby
0
 
LVL 4

Expert Comment

by:khsater
ID: 24823993
You should give each one a unique name and a unique id.  The name so that the submitted values can be differentiated and the id so that they're not confused by the browser.
0
 
LVL 39

Expert Comment

by:Roger Baklund
ID: 24824028
You could do like this:
function TimeDropdown($name) {
  $res = '<select name="'.$name.'">';
  for($i=0;$i<24*60;$i+=30) {
    $time = date('h:i A',mktime($i/60,$i%60,0));
    $res .= '<option value="'.$time.'">'.$time.'</option>';  
  }
  $res .= '</select>';
  return $res;
}
echo TimeDropdown('drop_off_time');
echo TimeDropdown('pick_up_time');

Open in new window

0
 
LVL 9

Author Comment

by:Rowby Goren
ID: 24824643
Hi cxr,

I used the below code.  

Please check the result.  

http://www.avrparking.com/test2.html

Any suggestions?

Thanks

function TimeDropdown($name) {
  $res = '<select name="'.$name.'">';
  for($i=0;$i<24*60;$i+=30) {
    $time = date('h:i A',mktime($i/60,$i%60,0));
    $res .= '<option value="'.$time.'">'.$time.'</option>';  
  }
  $res .= '</select>';
  return $res;
}
echo TimeDropdown('drop_off_time');
echo TimeDropdown('pick_up_time');

Open in new window

0
 
LVL 39

Expert Comment

by:Roger Baklund
ID: 24824751
You must sorround it with PHP tags:

<?php
  (php code here)
?>

You can (should) have three different php tags, first define the function, then put these where you want them in the form:

<?php echo TimeDropdown('drop_off_time'); ?>

<?php echo TimeDropdown('pick_up_time'); ?>

Your page has .html extension, normally you would need .php extension for the server to render PHP code, but this depends on the server configuration.
0
 
LVL 9

Author Comment

by:Rowby Goren
ID: 24824972
Would this be correct?
<?php 
function TimeDropdown($name) {
  $res = '<select name="'.$name.'">';
  for($i=0;$i<24*60;$i+=30) {
    $time = date('h:i A',mktime($i/60,$i%60,0));
    $res .= '<option value="'.$time.'">'.$time.'</option>';  
  }
  $res .= '</select>';
  return $res;
}
<?php echo TimeDropdown('drop_off_time'); ?>
<?php echo TimeDropdown('pick_up_time'); ?>	
?>

Open in new window

0
 
LVL 39

Expert Comment

by:Roger Baklund
ID: 24825061
No. You can not nest PHP tags. Split it in three parts: first the function within one set of PHP tags, then the two TimeDropdown() calls, each in its own PHP tags, placed where you want them in the HTML form.
<?php 
function TimeDropdown($name) {
  $res = '<select name="'.$name.'">';
  for($i=0;$i<24*60;$i+=30) {
    $time = date('h:i A',mktime($i/60,$i%60,0));
    $res .= '<option value="'.$time.'">'.$time.'</option>';  
  }
  $res .= '</select>';
  return $res;
}
?>
<form ... >
<?php echo TimeDropdown('drop_off_time'); ?>
(more form fields)
<?php echo TimeDropdown('pick_up_time'); ?>     
</form>

Open in new window

0
 
LVL 9

Author Comment

by:Rowby Goren
ID: 24825305
Working nicely. (I need to style it but it's working.

http://www.avrparking.com/test2.html

What field would I use for the form results?    I assume "drop_off_time" and "pick_up_time" ?  

I tried using the form but the date fields did not show up in the emailed form results.  OR is that something beyond the scope of this question -- for another Experts Exchange area?

Also (but it is not essential) is there a way to remove the "0" from times such as "09:30am"?

Rowby


0
 
LVL 9

Assisted Solution

by:xBellox
xBellox earned 300 total points
ID: 24825367
To change the zero leading hour to as "9:30 am", change the "h" to "g"  at line:

$time = date('g:i A',mktime($i/60,$i%60,0));

Open in new window

0
 
LVL 9

Expert Comment

by:xBellox
ID: 24825398
I saw in your test2.html that the two select input has tha same name: "drop_off_time". I think this may be the problem. And yes, normally the name of the variable should be $_POST["drop_off_time"] and $_POST["pick_up_time"] (or $drop_off_time and $pick_up_time if your server has the option "safe_mode"set to off).
0
 
LVL 9

Author Comment

by:Rowby Goren
ID: 24825558
Hi

I have to go to some meethings but will resume later.  Looks like all is gonna work fine!

Rowby
0
 
LVL 9

Author Comment

by:Rowby Goren
ID: 24831906
Hi xBellox:

Working very nice.  
http://avrparking.com/test2.html

One final question.

Can I add some code that will start the time at, say 8:00 am and end the time at say 10:00pm

Thanks

Rowby
0
 
LVL 39

Expert Comment

by:Roger Baklund
ID: 24832106
Just change the start and end time in the for loop, like this:

  for($i=8*60;$i<=22*60;$i+=30) {

22 == 10:00pm
0
 
LVL 9

Author Comment

by:Rowby Goren
ID: 24832308
I'm getting close but not quite there

The below code gives me a start time of 10:00 AM and an end time of 10:00 AM.

http://avrparking.com/test2.html

Rowby
<?php 
function TimeDropdown($name) {
  $res = '<select name="'.$name.'">';
   for($i=10*60;$i<=22*60;$i+=30) {
    $time = date('g:i A',mktime($i/60,$i%60,0));
    $res .= '<option value="'.$time.'">'.$time.'</option>';  
  }
  $res .= '</select>';
  return $res;
}
?>

Open in new window

0
 
LVL 9

Author Comment

by:Rowby Goren
ID: 24832317
Never mind. I think the code is correct.

Let me double check.

Stay tuned.
0
 
LVL 9

Author Closing Comment

by:Rowby Goren
ID: 31602074
Thanks all!
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question