Display 24 hours in a dropdown list using php - php

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.

Related

How to create dropdown programatically in php?

I want to create dropdown programmatically. The dropdown have time and at last I want in option close. Kindly check what I am doing:
<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>
This gives me the time what I want like
1:00 Am
2:00 AM
But I want at last the option "close" will show. Because office will close on sunday. I am getting the numbers programatically but at last I want user is able to select close option as well.
Any idea or suggestions would be welcome.
You could just do the following, this will add the close option after your code has added the hours:
<select>
<?php for($i = 0; $i < 24; $i++): ?>
<option value="<?= $i; ?>"><?= $i % 12 ? $i % 12 : 12 ?>:00 <?= $i >= 12 ? 'PM' : 'AM' ?></option>
<?php endfor ?>
<option value="close">Close</option>
</select>

get current year and five years before

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>

PHP add 30-minute increments to a time selection menu

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>

Echo in for loop

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.

Dealing with Time in an HTML form

Ok. I am working on a Program Schedule Manager for my internet radio station. I have come thus far but can't seem to figure out dealing with the Air Time and End Time of show when editing. Below is the code from the form.
The times are stored in the HTML database as 24-hour format to make sorting easier.
If you need to look at the rest of the code: https://github.com/phillf/ProgramScheduleManager
Code from: admin/editShow.php:
<td>What time does this show start?</td>
<td>
<select name="AirTime">
<?php for($i = 0; $i < 24; $i++): ?>
<option value="<?= $i; ?>"><?= $i % 12 ? $i % 12 : 12 ?>:00 <?= $i >= 12 ? 'pm' : 'am' ?></option>
<?php endfor ?>
</select>
</td>
<tr>
<tr>
<td>What time does the show end?</td>
<td>
<select name="EndTime">
<?php for($i = 0; $i < 24; $i++): ?>
<option value="<?= $i; ?>"><?= $i % 12 ? $i % 12 : 12 ?>:00 <?= $i >= 12 ? 'pm' : 'am' ?></option>
<?php endfor ?>
</select>
</td>
</tr>
Assuming you've done a database query and put the current air time in $airTime, you set the selected attribute like this:
<select name="AirTime">
<?php for($i = 0; $i < 24; $i++): ?>
<option value="<?= $i; ?>:00:00" <?php if ($i == $airTime) { echo 'selected'; } ?> ><?= $i % 12 ? $i % 12 : 12 ?>:00 <?= $i >= 12 ? 'pm' : 'am' ?></option>
<?php endfor ?>
</select>
And similarly for EndTime.

Categories