Reputation: 10553
I am using Laravel 5.8 and trying to validate a date
field. The name of the field is date_of_birth
and the validation is like the following,
$validatedData = $request->validate([
'date_of_birth' => 'date|before:today',
]);
I was using only date
rule but when it failed to validate i was trying with date|before:today
.
Both of the date rules are failing to for any year grater than 9999
. The validation i passing as success and MySQL
is showing the error as an invalid date.
Did i made any mistake on date validation or is it a bug?
Upvotes: 0
Views: 1034
Reputation: 856
if you try and convert a date higher than 10000 with DateTime natively the following occurs
$date = new DateTime("9999-12-31");
var_dump($date->format('Y-m-d'));
$date_2 = new DateTime("10000-01-01");
var_dump($date_2->format('Y-m-d'));
RESULT:
string(10) "9999-12-31"
string(10) "2000-01-01"
Notice that values higher than the year 10,000 present unexpected results.
However, if you are using a 64-bit version of PHP you can look for dates ~293billion years in either direction, see https://php.net/strtotime
The valid range of a timestamp is typically from Fri, 13 Dec 1901 20:45:54 UTC to Tue, 19 Jan 2038 03:14:07 UTC. (These are the dates that correspond to the minimum and maximum values for a 32-bit signed integer.)
Prior to PHP 5.1.0, not all platforms support negative timestamps, therefore your date range may be limited to no earlier than the Unix epoch. This means that e.g. dates prior to Jan 1, 1970 will not work on Windows, some Linux distributions, and a few other operating systems.
For 64-bit versions of PHP, the valid range of a timestamp is effectively infinite, as 64 bits can represent approximately 293 billion years in either direction.
You could probably create a custom validation rule, which validates the date against the number of seconds (assuming 64 bit PHP)
UPDATE: Looked at the documentation for validation and found
before:date
The field under validation must be a value preceding the given date. The dates will be passed into the PHP strtotime function. In addition, like the after rule, the name of another field under validation may be supplied as the value of date.
Interestingly, strtotime("10000-01-01 00:00:00") returns false, whilst strtotime("9999-12-31 23:59:59") returns an integer. the validation rule cannot parse the date.
Upvotes: 1