4

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>
TNK
  • 4,263
  • 15
  • 58
  • 81

5 Answers5

9

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.

Glitch Desire
  • 14,632
  • 7
  • 43
  • 55
5

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

Kermit
  • 33,827
  • 13
  • 85
  • 121
1

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>';
}
m59
  • 43,214
  • 14
  • 119
  • 136
  • I guess it's because it's identical to another answer. not my downvote though. – STT LCU Sep 05 '13 at 13:51
  • Perhaps because it's bad practice to echo out HTML? – Prisoner Sep 05 '13 at 13:52
  • It sure isn't identical. Bad practice? Since when? – m59 Sep 05 '13 at 13:52
  • Thanks for the very opinionated claim of bad practice and undeserved downvote! http://stackoverflow.com/questions/8318033/outputting-html-with-echo-considered-bad-practice-in-php My opinion is that ugly code is ugly. Oh wait...that's a fact.. – m59 Sep 05 '13 at 13:54
  • @m59 can you please pinpoint the relevant differences? i don't see them. – STT LCU Sep 05 '13 at 13:55
  • @m59 also, first you said that it's not bad practice and then link a question where the accepted answer begins with "I consider it to be bad practice"? – STT LCU Sep 05 '13 at 13:58
  • Because it's a matter of preference. My answer is by far neater than the other answer with the alternate syntax. As there is no performance concern or any other tradeoff, it is just a matter of preference. Objectively, mine is much shorter, especially because I had the sense to separate `$t` to make it easier on the eyes. – m59 Sep 05 '13 at 14:02
  • Oh, even better. By this reasoning, the original answer deserves an epic downvote. http://stackoverflow.com/questions/200640/are-php-short-tags-acceptable-to-use – m59 Sep 05 '13 at 14:05
  • @m59 Your answer will give `option value='12:00AM'` not `option value='0'`. Not my downvote but probably the reason. – Glitch Desire Sep 05 '13 at 14:49
  • Updated. Again, I found the php tags everywhere so distracting that I couldn't tell that was the intent. – m59 Sep 05 '13 at 14:53
1

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>
SaidbakR
  • 13,303
  • 20
  • 101
  • 195
0

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.

SeanWM
  • 16,789
  • 7
  • 51
  • 83