Php 如何在细枝中创建到小时的
嗨,我是Symfony和Twig的新手,所以我有个问题:Php 如何在细枝中创建到小时的,php,symfony,twig,Php,Symfony,Twig,嗨,我是Symfony和Twig的新手,所以我有个问题: 我如何在twig中创建这样的东西 for ($i = 0; $ <10; $i++) { //do something } for($i=0;$我会这样做 <select name="time"> {% set i = 0 %} {% for j in 0..12 %} <option value={{ i }} >{{ i }}</option>
我如何在twig中创建这样的东西
for ($i = 0; $ <10; $i++) {
//do something
}
for($i=0;$我会这样做
<select name="time">
{% set i = 0 %}
{% for j in 0..12 %}
<option value={{ i }} >{{ i }}</option>
{% set i = i + 5 %}
{% endfor %}
</select>
{%set i=0%}
{0..12%中j的百分比}
{{i}
{%set i=i+5%}
{%endfor%}
根据您的要求调整编号。在Twig中,您可以创建一个变量(甚至是一个数组)。在这种情况下,您可能需要创建一个字符串数组,然后像下面这样循环。请注意,这只是一种非常基本的方法
<label for="split-time">Please choose a 5-Minute Split Time:</label><br />
<select id="split-time">
{% set minutes = ["00", "05", "10", "15", "20", "25", "30", "35", "40", "45", "50", "55"] %}
{% for split in minutes %}
<option value="00:{{ split }}">00:{{ split }}</option>
{% endfor %}
</select>
请选择5分钟的分割时间:
{%设定分钟数=[“00”、“05”、“10”、“15”、“20”、“25”、“30”、“35”、“40”、“45”、“50”、“55”]%}
{分钟内拆分的百分比%}
00:{{split}
{%endfor%}
另一方面,您可能只想在控制器中执行一些准备工作,如下所示。假设您的操作是timeAction();您可以在Time Action中执行此操作:
public function timeAction() {
// GENERATE THE TIME INDICES:
$timeIndices = array();
for($i=0; $i < 60; $i+=5){
$val = $i;
if($i < 10){
$val = "0" . $i;
}
$timeIndices[] = $val;
}
return $this->render('YourBundle:ControllerName:time.html.twig', array(
'timeIndices' => $timeIndices
));
}
公共函数timeAction(){
//生成时间索引:
$timeindex=array();
对于($i=0;$i<60;$i+=5){
$val=$i;
如果($i<10){
$val=“0”。$i;
}
$timeindex[]=$val;
}
返回$this->render('YourBundle:ControllerName:time.html.twig',数组(
'TimeIndexes'=>$TimeIndexes
));
}
然后在Twig中,您可以执行以下操作:
<label for="split-time">Please choose a 5-Minute Split Time:</label><br />
<select id="split-time">
{% for timeIndex in timeIndices %}
<option value="00:{{ timeIndex }}">00:{{ timeIndex }}</option>
{% endfor %}
</select>
请选择5分钟的分割时间:
{TimeIndexes%中的timeIndex为%s}
00:{{timeIndex}}
{%endfor%}
最简单的方法是:
{% for minutes in range(0, 24 * 60 - 1, 5) %}
<option>{{ (minutes * 60)|date('H:i', false) }}</option>
{% endfor %}
{%(0,24*60-1,5)%范围内分钟的百分比}
{{(分钟*60){date('H:i',false)}
{%endfor%}
您可以根据需要更改输出格式。例如,如果您需要15分钟的步长并显示“am/pm”,您可以使用以下变量:
{% for minutes in range(0, 24 * 60 - 1, 15) %}
<option>{{ (minutes * 60)|date('h:i a', false) }}</option>
{% endfor %}
{%(0,24*60-1,15)%范围内分钟的百分比}
{{(分钟*60){date('h:ia',false)}
{%endfor%}
因此,您将获得如下选项:
<option>12:00 am</option>
<option>12:15 am</option>
<option>12:35 am</option>
12:00am
上午12:15
上午12:35
这是一个非常灵活和简单的解决方案
此外,您可以将秒(或分钟)作为选项的值,如下所示:
{% for minutes in range(0, 24 * 60 - 1, 5) %}
<option value="{{ minutes * 60 }}">{{ (minutes * 60)|date('H:i', false) }}</option>
{% endfor %}
{%(0,24*60-1,5)%范围内分钟的百分比}
{{(分钟*60){date('H:i',false)}
{%endfor%}
请阅读文档:
{% for minutes in range(0, 24 * 60 - 1, 5) %}
<option value="{{ minutes * 60 }}">{{ (minutes * 60)|date('H:i', false) }}</option>
{% endfor %}