What Is the Mm/Dd/Yyyy Regular Expression and How to Use It in PHP

What is the MM/DD/YYYY regular expression and how do I use it in php?

The problem is one of delimeters and escaped characters (as others have mentioned). This will work:

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

$test_date = '03/22/2010';
if(preg_match($date_regex, $test_date)) {
echo 'this date is formatted correctly';
} else {
echo 'this date is not formatted correctly';
}

Note that I added a forward-slash to the beginning and ending of the expression and escapped (with a back-slash) the forward-slashes in the pattern.

To take it one step further, this pattern won't properly extract the year... just the century. You'd need to change it to /(0[1-9]|1[012])[- \/.](0[1-9]|[12][0-9]|3[01])[- \/.]((?:19|20)\d\d)/ and (as Jan pointed out below) if you want to make sure the whole string matches (instead of some subset) you'll want to go with something more like /^(0[1-9]|1[012])[- \/.](0[1-9]|[12][0-9]|3[01])[- \/.]((?:19|20)\d\d)$/.


As others have mentioned, strtotime() might be a better option if you're just trying to get the date out. It can parse almost any commonly used format and it will give you a unix timestamp. You can use it like this:

$test_date = '03/22/2010';

// get the unix timestamp for the date
$timestamp = strtorime($test_date);

// now you can get the date fields back out with one of the normal date/time functions. example:
$date_array = getdate($timestamp);
echo 'the month is: ' . $date_array['month'];

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;
}

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}$/

regular expression to validate datetime format (MM/DD/YYYY)

Try your regex with a tool like http://jsregex.com/ (There is many) or better, a unit test.

For a native validation:

function validateDate(testdate) {
var date_regex = /^\d{2}\/\d{2}\/\d{4}$/ ;
return date_regex.test(testdate);
}

In your case, to validate (MM/DD/YYYY), with a year between 1900 and 2099, I'll write it like that:

function validateDate(testdate) {
var date_regex = /^(0[1-9]|1[0-2])\/(0[1-9]|1\d|2\d|3[01])\/(19|20)\d{2}$/ ;
return date_regex.test(testdate);
}

Validate Date format mm/dd/yyyy without any Regular Expression

make date into string by quotation

<?php
$is_valid_date = date('d/m/Y', strtotime('01-02-2014')) == "01/02/2014";

if( $is_valid_date){
echo 'Valid date';
}
else if ( !$is_valid_date)
{
echo 'invalid date';
}
?>

Regular Expression to check d/m/yyyy and dd/mm/yyyy - PHP

Apart from wether or not you should or should not use regex, this is the way:

\d{1,2}/\d{1,2}/\d{4}
  • \d means digits ( [0-9] )
  • {1,2} means 'the part before me should have at least 1 length, and maximum 2
  • {4} means exactly 4 characters long

Edit: This will match 1/2/2014 (both single), 11/12/2014 (both double) and 1/12/2014 (a single and a double).

best date regex mm/dd/yyyy

Use one of the DateTime.TryParse overloads (or DateTime.TryParseExact if you want more control) to validate whether a string is a valid representation of a DateTime.

DateTime dt;
if(DateTime.TryParse(someInputString, out dt)
{
// it is valid and dt can be used
}

This would be a better approach than regular expressions - it is faster, well tested and will produce a valid DateTime object.

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

i want php pattern for date as DD/MM/YYYY

You could use preg_match("/^\d{1,2}\/\d{1,2}\/\d{4}/",$date); however a regex isn't well-suited for this task because there you will still need to perform additional validation ("9999" is a valid year, for example). I think instead you may want to take a look at date_parse_from_format(), like:

date_parse_from_format('d/M/Y', $date);

See Convert String To date in PHP for more information.



Related Topics



Leave a reply



Submit