I'm trying to compare dates in my validation. The documentation says it's possible but It's not documented. I'm using annotations and I want one date to be later that the other. How do I do this?
How to compare dates in validation?
Asked Answered
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;
Use strtotime
You can convert the date format to timestap and then compare them both
You're mistaken. I'm using the symfony framework, which is a php framework. –
Obsession
I could if I wanted to just compare dates. But I'm locked into The Symfony-validation framework. I need to find out how to compare dates using annotations. –
Obsession
© 2022 - 2024 — McMap. All rights reserved.
Expression
validation, as stated in one of the answers – Acroter