Fahad Sadiq
Fahad Sadiq

Reputation: 77

TIme format validation in laravel against a particular format

I am trying to get date_format validation going on in my laravel application. Its an api and get datetime in the format

2015-10-09T12:36:576+01:00

It is supposed to be IOS 8601 format so I was checking it against the format "Y-m-d\TH:i:sO" by using

'createdAt'  => "required|date_format:Y-m-d\TH:i:sO",

But the validation fails. I am pretty sure that I am wrong in the format for the date(which I got from the official php documentation meaning the format for the data is not ISO 8601). Could someone please tell me what format that date is in?

Thanks

Upvotes: 3

Views: 7836

Answers (3)

majid behzadnasab
majid behzadnasab

Reputation: 123

in laravel 9 just use

'date' => ['required' , 'date'],
'date' => ['required' , 'date_format:Y/d/m'],

Upvotes: -2

Flayshon
Flayshon

Reputation: 373

Had the same problem just now. Checking against "Y-m-d\TH:i:sP" works for me.

From the PHP Docs:

P: Difference to Greenwich time (GMT) with colon between hours and minutes (added in PHP 5.1.3)

Upvotes: 3

DAB
DAB

Reputation: 1343

Your date format ends with +01:00. You are trying to match with type O which is +0100 (without the colon)

See the format specs here.

http://php.net/manual/en/function.date.php

What's kind of messed up is in laravel, it creates a date object which is able to accurately read your date, but then it tries to render it back to the format you specified (removing the :) and then comparing it to the original, which is obviously different:

https://github.com/laravel/framework/blob/5.4/src/Illuminate/Validation/Concerns/ValidatesAttributes.php#L363

This is a problem because technically both of those formats are ISO 8601 compliant.

Upvotes: 0

Related Questions