Simple question listing Time into ComboBox using PHP
As title I have a list long of time as below, how do I make it short instead of that crazy listing.
<select id="timesch">
<option>8.00am</option>
<option>8.30am</option>
<option>9.00am</option>
<option>9.30am</option>
<option>10.00am</option>
<option>10.30am</option>
<option>11.00am</option>
<option>11.30am</option>
<option>12.00pm</option>
<option>12.30pm</option>
<option>1.00pm</option>
<option>1.30pm</option>
<option>2.00pm</option>
<option>2.30pm</option>
<option>3.00pm</option>
<option>3.30pm</option>
<option>4.00pm</option>
<option>4.30pm</option>
<option>5.00pm</option>
<option>5.30pm</option>
<option>6.00pm</option>
<option>6.30pm</option>
<option>7.00pm</option>
<option>7.30pm</option>
<option>8.00pm</option>
<option>8.30pm</option>
<option>9.00pm</option>
<option>9.30pm</option>
<option>10.00pm</o开发者_C百科ption>
<option>10.30pm</option>
<option>11.00pm</option>
<option>11.30pm</option>
<option>12.00am</option>
</select>
You could generate the list of times like so...
$times = array();
$start = strtotime('8:00am');
$finish = strtotime('12:00am tomorrow');
while($start <= $finish) {
$times[] = date('g.ia', $start);
$start += strtotime('+30 min', 0);
}
var_dump($times);
CodePad.
Output
array(33) {
[0]=>
string(6) "8.00am"
[1]=>
string(6) "8.30am"
[2]=>
string(6) "9.00am"
[3]=>
string(6) "9.30am"
[4]=>
string(7) "10.00am"
[5]=>
string(7) "10.30am"
[6]=>
string(7) "11.00am"
[7]=>
string(7) "11.30am"
[8]=>
string(7) "12.00pm"
[9]=>
string(7) "12.30pm"
[10]=>
string(6) "1.00pm"
[11]=>
string(6) "1.30pm"
[12]=>
string(6) "2.00pm"
[13]=>
string(6) "2.30pm"
[14]=>
string(6) "3.00pm"
[15]=>
string(6) "3.30pm"
[16]=>
string(6) "4.00pm"
[17]=>
string(6) "4.30pm"
[18]=>
string(6) "5.00pm"
[19]=>
string(6) "5.30pm"
[20]=>
string(6) "6.00pm"
[21]=>
string(6) "6.30pm"
[22]=>
string(6) "7.00pm"
[23]=>
string(6) "7.30pm"
[24]=>
string(6) "8.00pm"
[25]=>
string(6) "8.30pm"
[26]=>
string(6) "9.00pm"
[27]=>
string(6) "9.30pm"
[28]=>
string(7) "10.00pm"
[29]=>
string(7) "10.30pm"
[30]=>
string(7) "11.00pm"
[31]=>
string(7) "11.30pm"
[32]=>
string(7) "12.00am"
You could then build your select
element accordingly.
Smart***, Comment Remove the newlines
<select id="timesch"><option>8.00am</option><option>8.30am</option><option>9.00am</option><option>9.30am</option><option>10.00am</option><option>10.30am</option><option>11.00am</option><option>11.30am</option><option>12.00pm</option><option>12.30pm</option><option>1.00pm</option><option>1.30pm</option><option>2.00pm</option><option>2.30pm</option><option>3.00pm</option><option>3.30pm</option><option>4.00pm</option><option>4.30pm</option><option>5.00pm</option><option>5.30pm</option><option>6.00pm</option><option>6.30pm</option><option>7.00pm</option><option>7.30pm</option><option>8.00pm</option><option>8.30pm</option><option>9.00pm</option><option>9.30pm</option><option>10.00pm</option><option>10.30pm</option><option>11.00pm</option><option>11.30pm</option><option>12.00am</option></select>
I wouldn't recommend changing it if you already have it created though. In fact, I would prefer to generate it once, and use that copy, instead of creating it on each page load. It also helps with readability.
Though I suppose you could use a php loop that adds 30 to a variable, divide that by 60, and if it's bigger then or equal to 1, subtract 12 and add the pm after.
Here is a php function that generates an option list (if that is the way you want to go):
function GenerateTimeSelect ()
{
echo '<select>';
for ($i=0;$i<60*24;$i=$i+30)
{
$hours = (int)($i/60);
$minutes = (($i%60 == 0)?00:30);
$isPM = false;
//Fix hours, our time system is really weird :/
if ($hours == 0)
$hours = 12;
if ($hours >= 13)
{
$hours -= 12;
$isPM = true;
}
echo '<option>'.$hours.'.'.(($minutes == 0)?'00':$minutes).(($isPM)?'PM':'AM').'</option>';
}
echo '</select>';
}
精彩评论