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

joelx's avatar
Level 3

Weird Date Validation

I have this validation

'required|date_format:m/Y'

then when I input '02/2016' (Note: That the month has 29 days) it will not return error but if I will input '02/2017' or year that february has only 28 days.. it will return error..

0 likes
3 replies
Snapey's avatar

I think its doing something with the current date, and failing now because its the 29th (or 30th, depending where you are).

The validator runs;

DateTime::createFromFormat('m/Y', '02/2017');

Which if I run in Tinker, returns

=> DateTime {#695
     +"date": "2017-03-01 23:07:38.000000",
     +"timezone_type": 3,
     +"timezone": "UTC",
   }
joelx's avatar
Level 3

ohhh i see.. so it is because the date now.. thank you

Snapey's avatar

Also explained here; http://stackoverflow.com/questions/36065963/unix-timestamp-from-custom-string-php

See the accepted answer.

This DateTime::createFromFormat('!m/Y', '02/2017');

produces the right result in Tinker

>>> $d = DateTime::createFromFormat('!m/Y', '02/2017');
=> DateTime {#736
     +"date": "2017-02-01 00:00:00.000000",
     +"timezone_type": 3,
     +"timezone": "UTC",

but does not work in validation

I think you might need a custom validation, or prefix your date with 01 and add day into the rule to check.

Please or to participate in this conversation.