Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

fjo24's avatar
Level 1

problems with validation of date in format (yyyy)

hello... i am working in a app, what register car, there ir a field called YEAR, i introduce four digits. with Carbon and the methods GetYEARAttribute and setYEARAttribute I could change it to the year format, use this date field to display it in the views. But in the form I need to validate it with the registration date. In the form I enter four digits corresponding to the year, everything works well .. except the validation. 'YEAR' => 'Before:create_at' ... I think the form does not understand the four digits.. thanks

0 likes
6 replies
mikefolsom's avatar

Why not treat year as an integer (smallint in database) rather than trying to cast it to a date, if that's the only part of the date you're interested in?

fjo24's avatar
Level 1

hi.. because i need validate with another date(created_at)... can i validate a integer with that date?

fjo24's avatar
Level 1

i need "year" before "create_at"

mikefolsom's avatar

One approach... ?

class MyController
{
    use Carbon;
    
    public function store(Request $request)
    {
        $createdYear = Carbon::parse($request->created_at)->year;

        $this->validate($request, [
            'year' => 'max:' . $createdYear,
        ]);

        // ...
    }
}
mikefolsom's avatar

The reasoning: Laravel validator's before rule uses PHP's strtotime function, which will parse a 4-digit value as a time rather than a year. So strtotime('2013') will return today's date at 8:13 p.m.

1 like

Please or to participate in this conversation.