如何比较验证中的日期?

时间:2016-04-20 12:48:38

标签: annotations symfony symfony-validator

我正在尝试比较验证中的日期。文档说这是可能的,但没有记录。 我正在使用注释,我希望一个日期晚于另一个。 我该怎么做?

enter image description here

2 个答案:

答案 0 :(得分:8)

I've eventually solved it using expressions like so:

     /**
     * @var \Datetime
     * @Assert\Type(
     *      type = "\DateTime",
     *      message = "vacancy.date.valid",
     * )
     * @Assert\GreaterThanOrEqual(
     *      value = "today",
     *      message = "vacancy.date.not_today"
     * )
     */
    private $startdate;

    /**
     * @var \Datetime
     * @Assert\Type(
     *      type = "\DateTime",
     *      message = "vacancy.date.valid",
     * )
     * @Assert\GreaterThanOrEqual(
     *      value = "today",
     *      message = "vacancy.date.not_today"
     * )
     * @Assert\Expression(
     *     "this.getEnddate() >= this.getStartdate()",
     *     message="vacancy.date.not_more_than"
     * )
     */
    private $enddate;

答案 1 :(得分:-1)

使用strtotime
您可以将日期格式转换为时间戳,然后将它们进行比较