Rowby Goren
asked on
Drop down time selector - is there an "elegant" way to do it in php
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
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
There we go. xBellox's method is much nicer than mine.
ASKER
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
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
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.
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');
ASKER
Hi cxr,
I used the below code.
Please check the result.
http://www.avrparking.com/test2.html
Any suggestions?
Thanks
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');
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_tim e'); ?>
<?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.
<?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_tim
<?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.
ASKER
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'); ?>
?>
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>
ASKER
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
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
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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).
ASKER
Hi
I have to go to some meethings but will resume later. Looks like all is gonna work fine!
Rowby
I have to go to some meethings but will resume later. Looks like all is gonna work fine!
Rowby
ASKER
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
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
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
for($i=8*60;$i<=22*60;$i+=
22 == 10:00pm
ASKER
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
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;
}
?>
ASKER
Never mind. I think the code is correct.
Let me double check.
Stay tuned.
Let me double check.
Stay tuned.
ASKER
Thanks all!
Open in new window