PHP - Display time in dropdown - php

I'd like some help please. I'm having this dropdown menu
<select name="schedule_time">
<option value="00:00:00">00:00:00</option>
<option value="00:30:00">00:30:00</option>
<option value="01:00:00">01:00:00</option>
<option value="01:30:00">01:30:00</option>
<option value="02:00:00">02:00:00</option>
<option value="02:30:00">02:30:00</option>
<option value="03:00:00">03:00:00</option>
<option value="03:30:00">03:30:00:00</option>
<option value="04:00:00">04:00:00</option>
<option value="04:30:00">04:30:00</option>
<option value="05:00:00">05:00:00</option>
<option value="05:30:00">05:30:00</option>
<option value="06:00:00">06:00:00</option>
<option value="06:30:00">06:30:00</option>
<option value="07:00:00">07:00:00</option>
<option value="07:30:00">07:30:00</option>
<option value="08:00:00">08:00:00</option>
<option value="08:30:00">08:30:00</option>
<option value="09:00:00">09:00:00</option>
<option value="09:30:00">09:30:00</option>
<option value="10:00:00">10:00:00</option>
<option value="10:30:00">10:30:00</option>
<option value="11:00:00">11:00:00</option>
<option value="11:30:00">11:30:00</option>
<option value="12:00:00">12:00:00</option>
<option value="12:30:00">12:30:00</option>
<option value="13:00:00">13:00:00</option>
<option value="13:30:00">13:30:00</option>
<option value="14:00:00">14:00:00</option>
<option value="14:30:00">14:30:00</option>
<option value="15:00:00">15:00:00</option>
<option value="15:30:00">15:30:00</option>
<option value="16:00:00">16:00:00</option>
<option value="16:30:00">16:30:00</option>
<option value="17:00:00">17:00:00</option>
<option value="17:30:00">17:30:00</option>
<option value="18:00:00">18:00:00</option>
<option value="18:30:00">18:30:00</option>
<option value="19:00:00">19:00:00</option>
<option value="19:30:00">19:30:00</option>
<option value="20:00:00">20:00:00</option>
<option value="20:30:00">20:30:00</option>
<option value="21:00:00">21:00:00</option>
<option value="21:30:00">21:30:00</option>
<option value="22:00:00">22:00:00</option>
<option value="22:30:00">22:30:00</option>
<option value="23:00:00">23:00:00</option>
<option value="23:30:00">23:30:00</option>
</select>
Is there a way to make it a little bit more dynamic, instead of having this huge html ?
Something like this
<?php foreach (date('H:i:s') as $time): ?>
<option value="<?php echo $time; ?>"><?php echo $time; ?></option>
<?php endforeach; ?>

You can use while loop to make it,
<?php
$start = "00:00"; //you can write here 00:00:00 but not need to it
$end = "23:30";
$tStart = strtotime($start);
$tEnd = strtotime($end);
$tNow = $tStart;
echo '<select name="schedule_time">';
while($tNow <= $tEnd){
echo '<option value="'.date("H:i:s",$tNow).'">'.date("H:i:s",$tNow).'</option>';
$tNow = strtotime('+30 minutes',$tNow);
}
echo '</select>';
?>

Best and simplest method:
<select>
<?php
for($hours=0; $hours<24; $hours++)
{
for($mins=0; $mins<60; $mins+=30)
{
$time = str_pad($hours,2,'0',STR_PAD_LEFT).':'.str_pad($mins,2,'0',STR_PAD_LEFT).':00';
echo '<option value= "'.$time.'">'.$time.'</option>';
}
}
?>
</select>

hey you can try this code, you can take whatever range $start and $end variable .
<?php
$start = "12:00:00";
$end = "24:00:00";
$tStart = strtotime($start);
$tEnd = strtotime($end);
$tNow = $tStart;
?>
<select name="schedule_time">
<?php
while($tNow <= $tEnd){ ?>
<option value="<?php echo date("H:i:s",$tNow); ?>"> <?php echo date("H:i:s",$tNow);?></option>
<?php
$tNow = strtotime('+30 minutes',$tNow);
}
?>

Try with this code.
<?php
$start = '00:00AM';
$end = '11:59PM';
$interval = '+30 minutes';
$now_str = strtotime($start);
$end_str = strtotime($end);
echo '<select name="schedule_time">';
while($now_str <= $end_str){
?>
<option value="<?php echo date('H:i A', $now_str);?>" ><?php echo date('H:i A', $now_str);?></option>
<?php
}
echo '</select>';
?>

Related

HTML select option selected does not work

I want to set the current date by default but it works only on year's select (the last one).
I wouldn't like to use javascript in this case. Just html and php.
I have already tried "selected='selected'" and "autocomplete=off" as shown in different other posts.
Here my code
<form id="form-orders-date" onsubmit="selectOrdersDate();">
<select id="orders-day">
<?php
for ($i=1; $i <= 31; $i++) {
if (sprintf('%02d', $i) == date("d")) {
echo "<option value=\"".sprintf('%02d', $i)."\" selected>".sprintf('%02d', $i)."</option>";
}
else{
echo "<option value=\"".sprintf('%02d', $i)."\">".sprintf('%02d', $i)."</option>" ;
}
}
?>
</select>
<select id="orders-month">
<?php
for ($i=1; $i <= 12; $i++) {
if (sprintf('%02d', $i) == date("m")) {
echo "<option value=\"".sprintf('%02d', $i)."\" selected>".sprintf('%02d', $i)."</option>";
}
else{
echo "<option value=\"".sprintf('%02d', $i)."\">".sprintf('%02d', $i)."</option>" ;
}
}
?>
</select>
<select id="orders-year">
<?php
for ($i=2012; $i <= 2020; $i++) {
if (sprintf('%02d', $i) == date("Y")) {
echo "<option value=\"".sprintf('%02d', $i)."\" selected>".sprintf('%02d', $i)."</option>";
}
else{
echo "<option value=\"".sprintf('%02d', $i)."\">".sprintf('%02d', $i)."</option>" ;
}
}
?>
</select>
<input type="submit" value="OK" />
</form>
here the plain HTML when the PHP has been run:
<form id="form-orders-date" onsubmit="selectOrdersDate();">
<select id="orders-day">
<option value="01">01</option>
<option value="02" selected>02</option>
<option value="03">03</option>
<option value="04">04</option>
<option value="05">05</option>
<option value="06">06</option>
<option value="07">07</option>
<option value="08">08</option>
<option value="09">09</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
<option value="13">13</option>
<option value="14">14</option>
<option value="15">15</option>
<option value="16">16</option>
<option value="17">17</option>
<option value="18">18</option>
<option value="19">19</option>
<option value="20">20</option>
<option value="21">21</option>
<option value="22">22</option>
<option value="23">23</option>
<option value="24">24</option>
<option value="25">25</option>
<option value="26">26</option>
<option value="27">27</option>
<option value="28">28</option>
<option value="29">29</option>
<option value="30">30</option>
<option value="31">31</option>
</select>
<select id="orders-month">
<option value="01">01</option>
<option value="02">02</option>
<option value="03" selected>03</option>
<option value="04">04</option>
<option value="05">05</option>
<option value="06">06</option>
<option value="07">07</option>
<option value="08">08</option>
<option value="09">09</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
</select>
<select id="orders-year">
<option value="2012">2012</option>
<option value="2013">2013</option>
<option value="2014">2014</option>
<option value="2015">2015</option>
<option value="2016">2016</option>
<option value="2017" selected>2017</option>
<option value="2018">2018</option>
<option value="2019">2019</option>
<option value="2020">2020</option>
</select>
<input type="submit" value="OK" />
</form>
This is the code you are looking for
Remember to use single quotes (') when including double quotes inside the PHP echo or else if using double quotes use " inside the outer double quotes.
<form id="form-orders-date" onsubmit="selectOrdersDate();">
<select id="orders-day">
<?php
for ($i=1; $i <= 31; $i++) {
if ($i == date("d")) {
echo '<option value="'.$i.'" selected>'.$i.'</option>';
}
else{
echo '<option value="'.$i.'">'.$i.'</option>';
}
}
?>
</select>
<select id="orders-month">
<?php
for ($i=1; $i <= 12; $i++) {
if ($i == date("m")) {
echo '<option value="'.$i.'" selected>'.$i.'</option>';
}
else{
echo '<option value="'.$i.'">'.$i.'</option>';
}
}
?>
</select>
<select id="orders-year">
<?php
for ($i=2012; $i <= 2020; $i++) {
if ($i == date("Y")) {
echo '<option value="'.$i.'" selected>'.$i.'</option>';
}
else{
echo '<option value="'.$i.'">'.$i.'</option>';
}
}
?>
</select>
Can you try like this.
<form id="form-orders-date" onsubmit="selectOrdersDate();">
<select id="orders-day">
<?php
for ($i=1; $i <= 31; $i++) {
if ($i == date("d")) {
echo "<option value='$i' selected='selected'>$i</option>";
} else {
echo "<option value='$i'>$i</option>";
}
}
?>
</select>
<select id="orders-month">
for ($i=1; $i <= 12; $i++) {
if ($i == date("m")) {
echo "<option value='$i' selected='selected'>$i</option>";
} else {
echo "<option value='$i'>$i</option>";
}
}
</select>
<select id="orders-year">
<?php
for ($i=2012; $i <= 2020; $i++) {
if ($i == date("Y")) {
echo "<option value='$i' selected='selected'>$i</option>";
} else {
echo "<option value='$i'>$i</option>";
}
}
?>
</select>
<input type="submit" value="OK" />
</form>
Here's a more compact way of achieving this:
<form id="form-orders-date" onsubmit="selectOrdersDate();">
<select id="orders-day">
<?php for ($d = 1; $d <= 31; $d++) { ?>
<option value="<?php echo $d; ?>" <?php if ($d == date('j')) { echo 'selected'; ?>><?php echo $d; ?></option>
<?php } ?>
</select>
<select id="orders-month">
<?php for ($m = 1; $m <= 12; $m++) { ?>
<option value="<?php echo $m; ?>" <?php if ($m == date('n')) { echo 'selected'; ?>><?php echo $m; ?></option>
<?php } ?>
</select>
<select id="orders-year">
<?php for ($y = 2012; $y <= 2020; $y++) { ?>
<option value="<?php echo $y; ?>" <?php if ($y == date('Y')) { echo 'selected'; ?>><?php echo $y; ?></option>
<?php } ?>
</select>
<input type="submit" value="OK" />
</form>
Have you tried autocomplete="off" for select? It works for me.
I know you said you tried this already, but your sample code doesn't show it. Here's how your tag should be formed:
<select>
<option value="01">01</option>
<option value="02" selected="selected">02</option>
<option value="03">03</option>
...
</select>
Your example above looks very different for the selected element. I'd suggest using whatever inspection tool is built into your web browser to see what exactly was rendered, and that will probably make it the solution obvious.
For reference, see:
Definition of select tag
Definition of option tag

Create a dropdown for months using a php function.

I am using this function to create a dropdown for months.
function formMonth(){
$month = strtotime(date('Y').'-'.date('m').'-'.date('j').' - 12 months');
$end = strtotime(date('Y').'-'.date('m').'-'.date('j').' + 0 months');
while($month < $end){
$selected = (date('F', $month)==date('F'))? ' selected' :'';
echo '<option'.$selected.' value="'.date('F', $month).'">'.date('F', $month).'</option>'."\n";
$month = strtotime("+1 month", $month);
}
}
My problem is I need to use numeric values of 1 to 12 for option values.
At this time it use month name for value options like this.
<select size="1" name="month">
<option selected value="January">January</option>
<option value="February">February</option>
<option value="March">March</option>
<option value="April">April</option>
<option value="May">May</option>
<option value="June">June</option>
<option value="July">July</option>
<option value="August">August</option>
<option value="September">September</option>
<option value="October">October</option>
<option value="November">November</option>
<option value="December">December</option>
</select>
Can anybody tell me how to modify this function.
Thank you.
Try below code:
<select name="month" size='1'>
<?php
for ($i = 0; $i < 12; $i++) {
$time = strtotime(sprintf('%d months', $i));
$label = date('F', $time);
$value = date('n', $time);
echo "<option value='$value'>$label</option>";
}
?>
</select>
If you take a look at the documentation for date(), you'll see under "Format" that you can use n for numeric representation of months, from 1 to 12. (You can also use m for having leading zeroes (01-12)).
This means that you basically only have to change
echo '<option'.$selected.' value="'.date('F', $month).'">'.date('F', $month).'</option>'."\n";
to
echo '<option'.$selected.' value="'.date('n', $month).'">'.date('F', $month).'</option>'."\n";
in your current code, provided that it otherwise works as expected (all I did there was replace date('F', $month) in the values-attribute to date('n', $month).
try this :your problem sloved.
<?php
function formMonth(){
$month = strtotime(date('Y').'-'.date('m').'-'.date('j').' - 12 months');
$end = strtotime(date('Y').'-'.date('m').'-'.date('j').' + 0 months');
$val=1;
echo "<select>";
while($month < $end){
$selected = (date('F', $month)==date('F'))? ' selected' :'';
echo '<option'.$selected.' value='.$val.'>'.date('F', $month).'</option>'."\n";
$month = strtotime("+1 month", $month);
$val++;
}
echo "</select>";
}
formMonth();
?>
Here is your answer:-
<?php
function months($selctedMonth='january'){
$months='<select name="month" size="1">';
for ($i = 12; $i > 0; $i--) {
$time = strtotime(sprintf('-%d months', $i));
$label = date('F', $time);
$selctedM = strtolower($selctedMonth) == strtolower($label) ? 'selected' : '';
$months.="<option value='$label' $selctedM >$label</option>";
}
$months.="</select>";
return $months;
}
echo months();
?>
By default, January will be displayed as selected.
if you write <?php echo months('march'); ?> then march will be selected by default.
<select size="1" name="month">
<option selected value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
</select>
If thats what you want.
Enjoy!
<div class="form-group">
<label>Month:</label>
<select id="month" name="month" class="form-control select2" style="width: 100%;">
<option value=""></option>
<?php
$monthArray = range(1, 12);
foreach ($monthArray as $month) {
$monthPadding = str_pad($month, 2, "0", STR_PAD_LEFT);
$fdate = date("F", strtotime("2015-$monthPadding-01"));
echo '<option value="'.$monthPadding.'">'.$fdate.'</option>';
}?>
</select>
</div>
For more deail click here
<select name="month">
<?php
foreach (['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'] as $monthNumber => $month) {
echo "<option value='$monthNumber'>{$month}</option>";
}
?>
</select>
I tried some of the answers here, and they have not given me the right solution to display an array of months.
For example, the #Kausha Mehta solution outputted this one.
<select name="month" size="1">
<option value="7">July</option>
<option value="8">August</option>
<option value="10">October</option>
<option value="10">October</option>
<option value="12">December</option>
<option value="12">December</option>
<option value="1">January</option>
<option value="3">March</option>
<option value="3">March</option>
<option value="5">May</option>
<option value="5">May</option>
<option value="7">July</option>
</select>
Which seems to skip some months and duplicating some months.
But this work as expected.
<select id="month" name="month" autocomplete='off'>
<?php
// Create a dropdown for months using a php foreach.
$monthArray = range(1, 12);
//::::
foreach ($monthArray as $month):
$monthCode = str_pad($month, 2, "0", STR_PAD_LEFT);
$selected = ($monthCode === date('m')) ? 'selected' : '';
//All months start with 1
$date = new DateTime(date("Y-").$monthCode."-01");
print '<option '.$selected.' value="'.$monthCode.'">'.$date->format('F').'</option>';
endforeach;
?>
</select>
for($i=1; $i<=12; $i++)
{
echo '<option value="'.date('m', strtotime('2020-'.$i.'-01')).'">'.date('M', strtotime('2020-'.$i.'-01')).'</option>';
}

Why does this php code not display my calendar given the year and month?

I select the dropdowns and nothing populates. I am not sure why? I am new to php so any help is much appreciated.
here is my code:
<section id="content" class="planner">
<select id="month" name="month">
<option value="0">--Select Month--</option>
<option value="January">January</option>
<option value="Februrary">February</option>
<option value="March">March</option>
<option value="April">April</option>
<option value="May">May</option>
<option value="June">June</option>
<option value="July">July</option>
<option value="August">August</option>
<option value="September">September</option>
<option value="October">October</option>
<option value="November">November</option>
<option value="December">December</option>
</select>
<select id="year" name="year">
<option value="0">--Select Year--</option>
<option value="2010">2010</option>
<option value="2011">2011</option>
<option value="2012">2012</option>
<option value="2012">2013</option>
<option value="2012">2014</option>
<option value="2012">2015</option>
<option value="2012">2016</option>
</select>
<table class="month">
<tr class="days">
<td>Mon</td>
<td>Tues</td>
<td>Wed</td>
<td>Thurs</td>
<td>Fri</td>
<td>Sat</td>
<td>Sun</td>
</tr>
<?php
if($_POST['month'] && $_POST['year'] != 0)
{
$today = date("d"); // Current day
$month=$_POST['month'];
$year=$_POST['year'];
GenerateCalendar($today,$month,$year);
}
function GenerateCalendar($today,$month,$year)
{
$today = date("d"); // Current day
$days = cal_days_in_month(CAL_GREGORIAN,$month,$year); // Days in current month
$lastmonth = date("t", mktime(0,0,0,$month-1,1,$year)); // Days in previous month
$start = date("N", mktime(0,0,0,$month,1,$year)); // Starting day of current month
$finish = date("N", mktime(0,0,0,$month,$days,$year)); // Finishing day of current month
$laststart = $start - 1; // Days of previous month in calander
$counter = 1;
$nextMonthCounter = 1;
if($start > 5){ $rows = 6; }else {$rows = 5; }
for($i = 1; $i <= $rows; $i++){
echo '<tr class="week">';
for($x = 1; $x <= 7; $x++){
if(($counter - $start) < 0){
$date = (($lastmonth - $laststart) + $counter);
$class = 'class="blur"';
}else if(($counter - $start) >= $days){
$date = ($nextMonthCounter);
$nextMonthCounter++;
$class = 'class="blur"';
}else {
$date = ($counter - $start + 1);
if($today == $counter - $start + 1){
$class = 'class="today"';
}
}
echo '<td '.$class.'><a class="date">'. $date . '</a></td>';
$counter++;
$class = '';
}
echo '</tr>';
}
}
?>
</table>
</section>
You are missing the html <form> tags around your select statements. You are also going to need an input type submit to send the form
<form action="" method="POST" name="myForm">
<!--YOUR SELECTS-->
<input type="submit" value="SEND THIS FORM"/>
</form>
the form action"" sends the post data to your current page. The method is either POST or GET. You are using POST.
That's all. You should consider learning HTML basics before continuing onto php.

How to create a dropdown of time?

I want to create a dropdown for time in PHP. It should be of 24 hour format with an interval of 30 minutes like 00:00 then 00:30 then 13:00 and so on.I want it round the clock.
I have used this code
<?php
$start = strtotime('12:00 AM');
$end = strtotime('11:59 PM');
?>
<select style="width:85px;" name="select1" id="select1">
<?php for($i = $start;$i<=$end;$i+=1800){ ?>
<option value='<?php echo date('G:i', $i); ?>'><?php echo date('G:i', $i); ?></option>;
<?php } ?>
</select>
The problem is that it doesn't cover the last span of 11:30 to 12:00 AM as the increment value exceeds the end, Is there a direct way to do this in PHP?
Uhm, it is extremely unlikely that in the future, our clocks suddenly gain a 25th hour, so a normal loop should do fine:
for($hours=0; $hours<24; $hours++) // the interval for hours is '1'
for($mins=0; $mins<60; $mins+=30) // the interval for mins is '30'
echo '<option>'.str_pad($hours,2,'0',STR_PAD_LEFT).':'
.str_pad($mins,2,'0',STR_PAD_LEFT).'</option>';
You could use that function to create time list anywhere on your page:
/**
*
* Get times as option-list.
*
* #return string List of times
*/
function get_times ($default = '19:00', $interval = '+30 minutes') {
$output = '';
$current = strtotime('00:00');
$end = strtotime('23:59');
while ($current <= $end) {
$time = date('H:i', $current);
$sel = ($time == $default) ? ' selected' : '';
$output .= "<option value=\"{$time}\"{$sel}>" . date('h.i A', $current) .'</option>';
$current = strtotime($interval, $current);
}
return $output;
}
Get times:
<select name="time"><?php echo get_times(); ?></select>
Here is the result:
<select name="time">
<option value="00:00">12.00 AM</option>
<option value="00:30">12.30 AM</option>
<option value="01:00">01.00 AM</option>
<option value="01:30">01.30 AM</option>
<option value="02:00">02.00 AM</option>
<option value="02:30">02.30 AM</option>
<option value="03:00">03.00 AM</option>
<option value="03:30">03.30 AM</option>
<option value="04:00">04.00 AM</option>
<option value="04:30">04.30 AM</option>
<option value="05:00">05.00 AM</option>
<option value="05:30">05.30 AM</option>
<option value="06:00">06.00 AM</option>
<option value="06:30">06.30 AM</option>
<option value="07:00">07.00 AM</option>
<option value="07:30">07.30 AM</option>
<option value="08:00">08.00 AM</option>
<option value="08:30">08.30 AM</option>
<option value="09:00">09.00 AM</option>
<option value="09:30">09.30 AM</option>
<option value="10:00">10.00 AM</option>
<option value="10:30">10.30 AM</option>
<option value="11:00">11.00 AM</option>
<option value="11:30">11.30 AM</option>
<option value="12:00">12.00 PM</option>
<option value="12:30">12.30 PM</option>
<option value="13:00">01.00 PM</option>
<option value="13:30">01.30 PM</option>
<option value="14:00">02.00 PM</option>
<option value="14:30">02.30 PM</option>
<option value="15:00">03.00 PM</option>
<option value="15:30">03.30 PM</option>
<option value="16:00">04.00 PM</option>
<option value="16:30">04.30 PM</option>
<option value="17:00">05.00 PM</option>
<option value="17:30">05.30 PM</option>
<option value="18:00">06.00 PM</option>
<option value="18:30">06.30 PM</option>
<option value="19:00" selected="">07.00 PM</option>
<option value="19:30">07.30 PM</option>
<option value="20:00">08.00 PM</option>
<option value="20:30">08.30 PM</option>
<option value="21:00">09.00 PM</option>
<option value="21:30">09.30 PM</option>
<option value="22:00">10.00 PM</option>
<option value="22:30">10.30 PM</option>
<option value="23:00">11.00 PM</option>
<option value="23:30">11.30 PM</option>
</select>
Why not use the DateTime object which is specifically designed for this type of thing?
$starttime = '00:00:00';
$time = new DateTime($starttime);
$interval = new DateInterval('PT30M');
$temptime = $time->format('H:i:s');
do {
echo $temptime . '<br />';
$time->add($interval);
$temptime = $time->format('H:i:s');
} while ($temptime !== $starttime);
$hoursArray = range(0, 12);
$minutesArray = range(0, 60);
$amPmArray = array('AM', 'PM');
//Pad the min and hr fields with a leading zero
array_walk($hoursArray, 'padLeadingZero');
array_walk($minutesArray, 'padLeadingZero');
/**
* Pad give input with a leading 0
* #param int $val Value that needs to be padded with a leading 0
*/
function padLeadingZero(&$val){
$val = str_pad($val, $padLength = 2, $padString = '0', $padType = STR_PAD_LEFT);
}
for joomla 2.* user
<field
name="time"
type="list"
required="true"
label="Time"
description="Time"
class="input-medium validate-time"
>
<option value="00:00:00">00:00</option>
<option value="00:30:00">00:30</option>
<option value="01:00:00">01:00</option>
<option value="01:30:00">01:30</option>
<option value="02:00:00">02:00</option>
<option value="02:30:00">02:30</option>
<option value="03:00:00">03:00</option>
<option value="03:30:00">03:30</option>
<option value="04:00:00">04:00</option>
<option value="04:30:00">04:30</option>
<option value="05:00:00">05:00</option>
<option value="05:30:00">05:30</option>
<option value="06:00:00">06:00</option>
<option value="06:30:00">06:30</option>
<option value="07:00:00">07:00</option>
<option value="07:30:00">07:30</option>
<option value="08:00:00">08:00</option>
<option value="08:30:00">08:30</option>
<option value="09:00:00">09:00</option>
<option value="09:30:00">09:30</option>
<option value="10:00:00">10:00</option>
<option value="10:30:00">10:30</option>
<option value="11:00:00">11:00</option>
<option value="11:30:00">11:30</option>
<option value="12:00:00">12:00</option>
<option value="12:30:00">12:30</option>
<option value="13:00:00">13:00</option>
<option value="13:30:00">13:30</option>
<option value="14:00:00">14:00</option>
<option value="14:30:00">14:30</option>
<option value="15:00:00">15:00</option>
<option value="15:30:00">15:30</option>
<option value="16:00:00">16:00</option>
<option value="16:30:00">16:30</option>
<option value="17:00:00">17:00</option>
<option value="17:30:00">17:30</option>
<option value="18:00:00">18:00</option>
<option value="18:30:00">18:30</option>
<option value="19:00:00">19:00</option>
<option value="19:30:00">19:30</option>
<option value="20:00:00">20:00</option>
<option value="20:30:00">20:30</option>
<option value="21:00:00">21:00</option>
<option value="21:30:00">21:30</option>
<option value="22:00:00">22:00</option>
<option value="22:30:00">22:30</option>
<option value="23:00:00">23:00</option>
<option value="23:30:00">23:30</option>
</field>
Convert your time into hours first and then by using for loop you can generate the list of time like you want.
$s_temp = new DateTime('17:00');
$stime = $s_temp->format('H');
$e_temp = new DateTime('03:30');
$etime = $e_temp->format('H');
$data = array();
if starting time is < 12
if($stime < 12){
for($hours=$stime; $hours<12; $hours++){
for($mins=0; $mins<60; $mins+=30){
$data[] = str_pad($hours,2,'0',STR_PAD_LEFT).':'.str_pad($mins,2,'0',STR_PAD_LEFT).' am';
}
}
for($hours=0; $hours<=$etime; $hours++){
for($mins=0; $mins<60; $mins+=30){
$data[] = str_pad($hours,2,'0',STR_PAD_LEFT).':'.str_pad($mins,2,'0',STR_PAD_LEFT).' pm';
}
}
}
if starting time is > 12
if($stime > 12){
for($hours=$stime; $hours<24; $hours++){
for($mins=0; $mins<60; $mins+=30){
$data[] = str_pad($hours,2,'0',STR_PAD_LEFT).':'.str_pad($mins,2,'0',STR_PAD_LEFT).' pm';
}
}
for($hours=0; $hours<=$etime; $hours++){
for($mins=0; $mins<60; $mins+=30){
$data[] = str_pad($hours,2,'0',STR_PAD_LEFT).':'.str_pad($mins,2,'0',STR_PAD_LEFT).' am';
}
}
}
return $data;
Variation of Christian's answer using AM and PM:
for ($hours=0; $hours<24; $hours++) { // the interval for hours is '1'
for($mins=0; $mins<60; $mins+=30) { // the interval for mins is '30'
$thehour = str_pad($hours,2,'0',STR_PAD_LEFT);
if ($thehour == "00") {
$thehour = "12";
}
if ($thehour > "12") {
$thehour = $thehour - 12;
if ($thehour < 10) {
$thehour = "0" . $thehour;
}
}
$theminutes = str_pad($mins,2,'0',STR_PAD_LEFT);
$mytime = $thehour.":".$theminutes;
if ($hours < 12) {
$mytime = $mytime . " AM";
}
else {
$mytime = $mytime . " PM";
}
echo "<option>".$mytime."</option>";
}
}

PHP drop down menu

I am doing a select on the database and some time it returns me 10 records and sometimes 1000, depending on the criteria of search. What I want is a drop down that will have break after every 30 records. something like this
<select id="dd" >
<option value="0">1-30</option>
<option value="30">31-60</option>
<option value="60">61-90</option>
<option value="91">91-120</option>
</select>
How do I do that in PHP dynamically?
Try this :)
<select id="dd" >
<?php
$start = 0;
$end = mysql_num_rows($resultquery);
while ( ($start+30) < $end || ($end-$start)>0)
echo "<option value=\"". $start . "\">". $start ."-" . ($start+=30) . "</option>";
?>
</select>
if $end = 95; output will be:
<select id="dd" >
<option value="0">0-30</option>
<option value="30">30-60</option>
<option value="60">60-90</option>
<option value="90">90-120</option>
</select>
Here's my take on it:
echo '<select id="dd">';
$j = 0;
for($i=0;$i<$numResults;$i++)
{
if($i%30==0)
echo '<option value="'.$j.'">'.($j+1).'-'.($j+=30).'</option>';
}
echo '</select>';
for $numResults == 130 output would be:
<select id="dd">
<option value="0">1-30</option>
<option value="30">31-60</option>
<option value="60">61-90</option>
<option value="90">91-120</option>
<option value="120">121-150</option>
</select>
Maybe something along the lines of this:
<select id="dd" >
<?php
$rows = mysql_num_rows($result);
for($count=0;$count<$rows;$count++)
{
if($count % 30 == 0)
{
$end = $count+30;
if($end > $rows)
$end = $rows;
echo '<option value="'.$count.'">'.$count.'-'.$end.'</option>';
}
}
?>
</select>
This is untested.

Categories