如何自动增加表单选项字段?

时间:2016-05-05 18:38:12

标签: php

如何在php中以15min的增量自动增加以下内容?

<option value="9:00am" <?= ($item['starttime']) == '9:00am' ? 'selected' : '' ?>>9:00am</option>

php输出应如下所示:

<option value="9:00am" <?= ($item['starttime']) == '9:00am' ? 'selected' : '' ?>>9:00am</option>
<option value="9:15am" <?= ($item['starttime']) == '9:15am' ? 'selected' : '' ?>>9:15am</option>
<option value="9:30am" <?= ($item['starttime']) == '9:30am' ? 'selected' : '' ?>>9:30am</option>
etc, etc

3 个答案:

答案 0 :(得分:2)

这是你想要的:

<?php

for ($i = 0; $i <= 23; $i++)
{
$postfix = "am";
$k = $i;
if ($i >= 12)
    {
    $postfix = "pm";
    }

if ($i > 12) $k = $i - 12;
for ($j = 0; $j <= 45; $j+= 15)
    {
    $time = $k . ':' . str_pad($j, 2, '0', STR_PAD_LEFT) . $postfix;
    $selected = "";
    if ($item['starttime'] == $time) $selected = "selected";
    echo '<option  value="' . $time . '" ' . $selected . '>' . $time . '</option>';
    }
}

?>

答案 1 :(得分:1)

另一种方式是:

UpdateDialog.js:108 setting up render for update dialog with state 
ListBoxWidget.js:11 **** calling to set state in constructor with 
ListBoxWidget.js:26 render in list box widget
ListBoxWidget.js:28 this.state.selected inside widget--
ListBoxWidget.js:29 this.props.selectedListObjects inside widget--
UpdateDialog.js:108 setting up render for update dialog with state 
ListBoxWidget.js:11 **** calling to set state in constructor with 
ListBoxWidget.js:26 render in list box widget
ListBoxWidget.js:28 this.state.selected inside widget--
ListBoxWidget.js:29 this.props.selectedListObjects inside widget--
ListBoxWidget.js:21 ---- calling mount 
UpdateDialog.js:78 calling componentDidMount in update
UpdateDialog.js:41 this.props.object.entity._links.testCases.href http://localhost:8081/api/testSuites/1/testCases
ListBoxWidget.js:21 ---- calling mount 
UpdateDialog.js:78 calling componentDidMount in update
UpdateDialog.js:41 this.props.object.entity._links.testCases.href http://localhost:8081/api/testSuites/2/testCases
UpdateDialog.js:57 objectMap
UpdateDialog.js:58 [Promise, Promise]
UpdateDialog.js:57 objectMap
UpdateDialog.js:58 [Promise]
UpdateDialog.js:63 objects
UpdateDialog.js:64 [Object, Object]
UpdateDialog.js:65 calling for each
UpdateDialog.js:67 testCase._links.self.hrefhttp://localhost:8081/api/testCases/1
UpdateDialog.js:67 testCase._links.self.hrefhttp://localhost:8081/api/testCases/3
UpdateDialog.js:108 setting up render for update dialog with state http://localhost:8081/api/testCases/1,http://localhost:8081/api/testCases/3
ListBoxWidget.js:26 render in list box widget
ListBoxWidget.js:28 this.state.selected inside widget--
ListBoxWidget.js:29 this.props.selectedListObjects inside widget-http://localhost:8081/api/testCases/1,http://localhost:8081/api/testCases/3-
UpdateDialog.js:63 objects
UpdateDialog.js:64 [Object]
UpdateDialog.js:65 calling for each
UpdateDialog.js:67 testCase._links.self.hrefhttp://localhost:8081/api/testCases/1
UpdateDialog.js:108 setting up render for update dialog with state http://localhost:8081/api/testCases/1
ListBoxWidget.js:26 render in list box widget
ListBoxWidget.js:28 this.state.selected inside widget--
ListBoxWidget.js:29 this.props.selectedListObjects inside widget-http://localhost:8081/api/testCases/1-

请参阅以下链接了解日期格式 http://php.net/manual/en/function.date.php

答案 2 :(得分:0)

这是一种方法:

<select>
<?php
$t = "09:00:00";                 // START TIME.
$item['starttime'] = "10:45:00"; // SELECTED TIME.
for ( $i = 0; $i < 10; $i++ )    // DISPLAY 10 ITEMS.
{ $endTime = strtotime("+15 minutes", strtotime($t) ); // ADD 15 MINUTES.
  $t = date('h:i:s', $endTime);  // CONVERT TIME TO STRING.
  echo "<option value='$t' " .
       (($item['starttime'] == $t) ? 'selected' : '') .
       ">$t</option>\n";
}
?>
</select>