如何在symfony2中选择一年

时间:2016-01-15 14:00:46

标签: symfony

我想做一个选择,显示从1960年到今年的年份,所以在我提交之后我应该有一个字符串如:1989。

我试过这样但是它不起作用:

    If Divider >= 90 Then
        Console.WriteLine("Your average Grade is an A, and your average in numbers is {0}", Divider)
    ElseIf Divider >= 80 Then
        Console.WriteLine("Your average Grade is a B, and your average in numbers is {0}", Divider)
    ElseIf Divider >= 70 Then
        Console.WriteLine("Your average Grade is a C, and your average in numbers is {0}", Divider)
    ElseIf Divider >= 60 Then
        Console.WriteLine("Your average Grade is a D, and your average in numbers is {0}", Divider)
    Else
        Console.WriteLine("Your average Grade is an F, and your average in numbers is {0}", Divider)
    End If

2 个答案:

答案 0 :(得分:7)

这很简单。
如果在提交表单之后你想要一个年份字符串,那么自1960年以来就生成一系列年份,如下所示:

class MyClassType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('year',
                'Symfony\Component\Form\Extension\Core\Type\ChoiceType',[
                'choices' => $this->getYears(1960)
            ])
            # or (for symfony <= 2.7):
            # ->add('year', 'choice', ['choices' => $this->getYears(1960)])

        ;
    }

    private function getYears($min, $max='current')
    {
         $years = range($min, ($max === 'current' ? date('Y') : $max));

         return array_combine($years, $years);
    }
}

模板:

enter image description here

表单提交后:

enter image description here

答案 1 :(得分:0)

使用具有此模式的DateInterval类PHP:

$interval = new DateInterval('P30Y');

参考日期开始