I need a select tag with current year and five years before
<select class='selrange' id='rangeyeara'>
<?php
for($y=date("Y")-5; $y=date("Y"); $y++){
echo "<option>" . $y . "</option>";
}
?>
</select>
current year is there but there is no any other one.
The second statement in your for loop need to be the "end" condition.
Change the operator = (assignation) to <= (comparison):
<select class='selrange' id='rangeyeara'>
<?php
for($y=date("Y")-5; $y<=date("Y"); $y++){
echo "<option>" . $y . "</option>";
}
?>
</select>
Getting the year is quite easy in PHP.
<select class='selrange' id='rangeyeara'>
<?php $currentYear = date('Y'); ?>
<?php for ($year = $currentYear - 5; $year <= $currentYear; $year++): ?>
<option value="<?= $year; ?>"><?= $year; ?></option>
<?php endfor; ?>
</select>
Related
Forgive me If I posted in the wrong forum or this question has been answered before. I did my research and I couldn't find an answer. I need to create a time selection menu that user can select times from in increments of 30 minutes using PHP/HTML(See image 1):
I created the time selection menu using the code below, but I don't know how to add the 30-minute increments to each time (See Image 2). Can someone please help me?
<tr><td>Time</td>
<td><select name="times" multiple>
<?php for($i = 7; $i < 20; $i++): ?>
<option value="<?= $i; ?>"><?= $i % 12 ? $i % 12 : 12 ?>:00 <?= $i >= 12 ? 'pm' : 'am' ?></option>
<?php endfor ?>
</select></td>
</tr>
Image 1
Image 2
This does the trick:
<tr><td>Time</td>
<td><select name="times" multiple>
<?php
$start = strtotime('07:00');
$end = strtotime('20:00');
for ($i=$start; $i<=$end; $i = $i + 30*60){
echo '<option>'.date('g:i A',$i).'</option>';
}
?>
</select></td>
</tr>
this should be the better way to do this, as you have already done the main part and just wanted to add the 30 minute interval, so by using your code i am just expanding it in the following way:
<tr><td>Time</td>
<td><select name="times" multiple>
<?php for($i = 7; $i < 20; $i++): ?>
<?php if ($i % 12): ?>
<option value="<?= $i; ?>"><?= $i % 12 ?>:00 <?= $i >= 12 ? 'pm' : 'am' ?></option>
<option value="<?= $i; ?>:30"><?= $i % 12 ?>:30 <?= $i >= 12 ? 'pm' : 'am' ?></option>
<?php else: ?>
<option value="<?= $i; ?>"><?= 12 ?>:00 <?= $i >= 12 ? 'pm' : 'am' ?></option>
<option value="<?= $i; ?>:30"><?= 12 ?>:30 <?= $i >= 12 ? 'pm' : 'am' ?></option>
<?php endif; ?>
<?php endfor ?>
</select></td>
</tr>
This is my code:
<?php
$date = 2015-02-30;
$year = substr($date, 0, 4);
$month = substr($date, 5, 2);
$day = substr($date, 8, 2);
?>
<select>
<?php
for ($i=1; $i < 31; $i++) { ?>
<option value="<?php echo $i; ?>" <?php if($day === $i){ echo "selected"; }; ?>><?php echo $i; ?></option>
<?php } ?>
</select>
For the option, the number 4 should be selected. Why doesn't it work? Thanks
Sorry, I already had this in a select statement
EDIT: See the code edit above. Maybe because the
You need to wrap your code in a select statement!
An option statement wont work without the select tag around it:
<html>
<body>
<select> <!-- Start the select statement -->
<!-- Your Code -->
<?php
$num = 4;
for ($i=1; $i < 10; $i++)
{
?>
<option value="<?php echo $i; ?>" <?php if($num === $i){ echo "selected"; }; ?>><?php echo $i; ?></option>
<?php
}
?>
<!-- End your code -->
</select> <!-- End the select statement -->
</body>
</html>
After your edit, it looks like this is your main problem:
$date = 2015-02-30;
This is not what you think it is. It should be quoted like this:
$date = '2015-02-30';
Otherwise, $date is a not a string, it's a math expression that evaluates to (int) 1983, so substr($date, 8, 2); will evaluate to false, not 30, and then obviously your option won't be selected.
html:
<select class="col-lg-4 col-xs-12 col-sm-5" name="fromYear" ><?php $starting_year =date('Y', strtotime('-50 year'));$ending_year = date('Y', strtotime('+4 year'));$current_year = date('Y');$selected_year = date('y',$profile->fromYear);for($starting_year; $starting_year <= $ending_year; $starting_year++) { echo '<option value="'.$starting_year.'"';if( $selected_year ) {echo ' selected="selected"'; }echo ' >'.$starting_year.'</option>';}?><select>
this code dynamically generates Dropdown for Year. How can I get year selected from the database in this code? for example,if i select 2009 in databse than this code display 2009 as selected
You don't need to do "$selected_year = date('Y',$profile->fromYear);" please check my code
<?php
$starting_year = date('Y', strtotime('-50 year'));
$ending_year = date('Y', strtotime('+4 year'));
$current_year = date('Y');
$selected_year = $profile->fromYear; //previosly selected year
?>
<select class="col-lg-4 col-xs-12 col-sm-5" name="fromYear" >
<?php
for($starting_year; $starting_year <= $ending_year; $starting_year++) {
echo '<option value="'.$starting_year.'"';
if($selected_year == $starting_year){
echo ' selected="selected"';
}
echo ' >'.$starting_year.'</option>';
}?>
I think you want the selected year as the current year please check my code
<select class="col-lg-4 col-xs-12 col-sm-5" name="fromYear" >
<?php
$starting_year =date('Y', strtotime('-50 year'));
$ending_year = date('Y', strtotime('+4 year'));
$current_year = date('Y');
$selected_year = date('Y',$profile->fromYear);
for($starting_year; $starting_year <= $ending_year; $starting_year++)
{
echo '<option value="'.$starting_year.'"';
if( $selected_year == $starting_year)
{
echo ' selected="selected"';
}
echo ' >'.$starting_year.'</option>';
}
?>
<select>
I want to display some number in a dropdown options with the increment 5 which will be started to 5 and ended at 1005. The result should be 5, 10, 15, .... 1000, 1005.
So I wrote the loop as
<?php for ($i=5; $i < 1005; $i++) { ?>
<option value = "<?php echo $i; ?>"><?php echo $i; ?></option>
<?php } ?>
But it gives me as 5,6,7,.... . How can I resolve it?
<?php for ($i=5; $i < 1005; $i+=5) { ?>
<option value = "<?php echo $i; ?>"><?php echo $i; ?></option>
<?php } ?>
$i+=5 <-- This is the only change.
How about this?
I am trying to create a dropdown list for display time. My Option tag should be something like this
<option value="1">01.00AM</option>
<option value="2">02.00AM</option>
<option value="3">03.00AM</option> and so on
So Can I know is there a quick way to create the array instead of typing each hours in option tag?
NOTE: AM and PM should be display according to the time.
I tried it with this code, but it doesn't work for me..
<select>
<?php for($i = 0; $i < 24; $i++): ?>
<option value="<?= $i; ?>"><?= $i % 12 ? $i % 12 : 12 ?>:00 <?= $i >= 12 ? 'pm' : 'am' ?></option>
<?php endfor ?>
</select>
A slight variant on Fresh Prince's deleted answer
I give Fresh Prince of SO most of the credit for this one, but since he deleted his original answer I'm posting a variant of it without the mixed echo tags and weird concatenations.
<select id='time'>
<?php for($i = 1; $i <= 24; $i++): ?>
<option value="<?= $i; ?>"><?= date("h.iA", strtotime("$i:00")); ?></option>
<?php endfor; ?>
</select>
Note: I am using short echo tags <?= because the original post used them. I'd recommend replacing these with <?php echo if you're writing portable code that needs to support older versions of PHP.
Output format
The output format from this script is:
<select id='time'>
<option value="1">01.00AM</option>
<option value="2">02.00AM</option>
...
<option value="23">11.00PM</option>
<option value="24">12.00AM</option>
</select>
This is a live demo.
Why not just use strtotime and date?
date("h.iA", strtotime($i . ":00"))
See a demo
Working it into your example,
<?php
for($i = 0; $i < 24; $i++):
echo "<option value=\"$i\">" . date("h.iA", strtotime($i . ":00")) . "</option>\n";
endfor;
?>
Alternatively,
<?php
for($i = 0; $i < 24; $i++, $d = date("h.iA", strtotime($i . ":00:00"))) {
echo "<option value=\"$i\">$d</option>\n";
}
?>
Lastly,
for($i = 0; $i < 24; print "<option value=\"$i\">" . date("h.iA", strtotime($i . ":00:00")) . "</option>\n", $i++);
See a demo
Checkout the following demo: http://phpfiddle.org/main/code/8y7-hut In-which sprintf formating features are used to fill preleading 0.
The following is the code used:
<select>
<?php for($i = 0; $i < 24; $i++): ?>
<option value="<?php echo $i+1; ?>"><?php printf('%1$02d.00',(($i+1) > 12)? ($i+1)-12 : $i+1)?><?php echo (($i < 12)? 'AM' : 'PM'); ?></option>
<?php endfor ?>
</select>
No need for all those messy php tags.
for ($i=0; $i<24; ++$i) {
$t = date("H.iA", strtotime($i.":00:00"));
echo '<option value="'.$i.'">'.$t.'</option>';
}
Try this:
$start = '12:00AM';
$end = '11:59PM';
$interval = '+1 hour';
// $interval = '+30 minutes';
// $interval = '+15 minutes';
$start_str = strtotime($start);
$end_str = strtotime($end);
$now_str = $start_str;
echo '<select>';
while($now_str <= $end_str){
echo '<option value="' . date('h:i A', $now_str) . '">' . date('h:i A', $now_str) . '</option>';
$now_str = strtotime($interval, $now_str);
}
echo '</select>';
Here's a working example.