PHP Regex to Check Date Is in Yyyy-Mm-Dd Format

PHP Regex to check date is in YYYY-MM-DD format

Try this.

$date="2012-09-12";

if (preg_match("/^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$/",$date)) {
return true;
} else {
return false;
}

PHP preg_match yyyy-mm-dd

You can use this:

if ( preg_match('/^[0-9]{4}-[0-9]{2}-[0-9]{2}\z/', $value) ) { ...

however this kind of pattern can validate something like 0000-40-99

pattern details:

/         # pattern delimiter
^ # anchor: start of the string
[0-9]{4} # four digits
- # literal: -
[0-9]{2}
-
[0-9]{2}
\z # anchor: end of the string
/ # pattern delimiter

Regular Expression Help for Date Validation - dd/mm/yyyy - PHP

I think you should escape the slashes /^[0-9]{1,2}\/[0-9]{1,2}\/[0-9]{4}$/

Regex to validate date in PHP using format as YYYY-MM-DD

Your regex didn't work because you had unescaped / delimiter.

The regex that would validate date in format YYYY-MM-DD as follows:

^(19|20)\d\d[\-\/.](0[1-9]|1[012])[\-\/.](0[1-9]|[12][0-9]|3[01])$

It will validate that the year starts with 19 or 20, that the month is not greater than 12 and doesn't equal 0 and that the day is not greater than 31 and doesn't equal 0.

Example Online

Using your initial example, you could test it like this:

$date_regex = '/^(19|20)\d\d[\-\/.](0[1-9]|1[012])[\-\/.](0[1-9]|[12][0-9]|3[01])$/';
$hiredate = '2013-14-04';

if (!preg_match($date_regex, $hiredate)) {
echo '<br>Your hire date entry does not match the YYYY-MM-DD required format.<br>';
} else {
echo '<br>Your date is set correctly<br>';
}

Example Online

Regular Expression to match dates in YYYY-MM-DD format

What you need is anchors, specifically ^ and $. The former matches the beginning of the string, the latter matches the end.

The other point I would make is the [] are unnecessary. \d retains its meaning outside of character ranges.

So your regex should look like this: /^\d{4}-\d{2}-\d{2}$/.

Correctly determine if date string is a valid date in that format

You can use DateTime::createFromFormat() for this purpose:

function validateDate($date, $format = 'Y-m-d')
{
$d = DateTime::createFromFormat($format, $date);
// The Y ( 4 digits year ) returns TRUE for any integer with any number of digits so changing the comparison from == to === fixes the issue.
return $d && $d->format($format) === $date;
}

[Function taken from this answer. Also on php.net. Originally written by Glavić.]


Test cases:

var_dump(validateDate('2013-13-01'));  // false
var_dump(validateDate('20132-13-01')); // false
var_dump(validateDate('2013-11-32')); // false
var_dump(validateDate('2012-2-25')); // false
var_dump(validateDate('2013-12-01')); // true
var_dump(validateDate('1970-12-01')); // true
var_dump(validateDate('2012-02-29')); // true
var_dump(validateDate('2012', 'Y')); // true
var_dump(validateDate('12012', 'Y')); // false

Demo!

regex to get date yyyy-mm-dd from any string

To match dates wherever they appear, remove the $ and ^ anchors from your original regex.

To match dates at the start of any input remove the $ at the end (leave the ^).

You can also put the remaining pattern inside parentheses for convenience, so that the match is also captured as a whole.

Your suggested improvement has a spurious dot at the end which will match any character; that was the reason for returning matches with three-digit days.

Regex to validate date formats dd/mm/YYYY, dd-mm-YYYY, dd.mm.YYYY, dd mmm YYYY, dd-mmm-YYYY, dd/mmm/YYYY, dd.mmm.YYYY with Leap Year Support

The regex you pasted does not validate leap years correctly, but there is one that does in the same post.
I modified it to take dd/mm/yyyy, dd-mm-yyyy or dd.mm.yyyy.

^(?:(?:31(\/|-|\.)(?:0?[13578]|1[02]))\1|(?:(?:29|30)(\/|-|\.)(?:0?[13-9]|1[0-2])\2))(?:(?:1[6-9]|[2-9]\d)?\d{2})$|^(?:29(\/|-|\.)0?2\3(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))$|^(?:0?[1-9]|1\d|2[0-8])(\/|-|\.)(?:(?:0?[1-9])|(?:1[0-2]))\4(?:(?:1[6-9]|[2-9]\d)?\d{2})$

I tested it a bit in the link Arun provided in his answer and also here and it seems to work.

Edit February 14th 2019: I've removed a comma that was in the regex which allowed dates like 29-0,-11

How to differentiate date formats as dd/mm/yy or dd/mm/yyyy?

Problems:

1. This $ should be in end instead of ^. $ is for end of string.

2. Don't forget delimiters /.

Regular expression:

/^\d{1,2}\/\d{1,2}\/\d{4}$/ for strings like 10/10/1111

/^\d{1,2}\/\d{1,2}\/\d{2}$/ for strings like 10/10/11

Try this code snippet here

Preg_match (yyyy-mm-dd hh:mm:ss) - only this format

Add start ^ and end $ anchors to your regex.

if (preg_match("/^(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})$/", $date_time))


Related Topics



Leave a reply



Submit