Carbon: Check if a string is a valid date with a given format

Created on 12 Sep 2016  路  4Comments  路  Source: briannesbitt/Carbon

It would be very useful to be able to check if a string is a valid date. It is a very common use case to get a date string from a form or from an external service.

Example from moment.js: moment('29.2.2013', 'DD.MM.YYYY').isValid()

Code which I believe moment.js uses for this:
https://github.com/moment/moment/blob/c587bf0063c18214238ca83f3f9c6170daf25bed/src/lib/create/from-string.js

Most helpful comment

Feel free to open a PR @loota

All 4 comments

A PR for that would be much appreciated @loota !

Respect\Validation also validates this:
use Respect\Validation\Validator as v;

var_dump(
v::date('d.m.Y')->validate('30.6.2015'), //true
v::date('d.m.Y')->validate('31.6.2015'), //false
v::date('d.m.Y')->validate('29.2.2015'), //false
v::date('d.m.Y')->validate('29.2.2016') //true
);
?>

The code seems to be here: https://github.com/Respect/Validation/blob/master/library/Rules/Date.php

Feel free to open a PR @loota

shame

Was this page helpful?
0 / 5 - 0 ratings