How to Create the MM/DD/YYYY Date Regular Expression in PHP

Subodh Poudel Feb 02, 2024
How to Create the MM/DD/YYYY Date Regular Expression in PHP

This tutorial will demonstrate the way to create a regular expression for the mm/dd/yyyy date format in PHP.

Create the MM/DD/YYYY Date Regular Expression in PHP

We can use the regular expression to validate the user input according to the rules applied in the expression. We can validate any inputs using the regular expression, and we can even validate the mm/dd/yyyy date format using the regular expression.

We can create a regular expression and store it in a variable. We will use the preg_match() function to test random date format inputs with the regular expression.

A regex pattern for the date format mm/dd/yyyy is shown below. We will discuss it in detail.

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

A regex pattern should always have a correct delimiter around it. Here, the ~ symbol is the delimiter surrounding the pattern.

Next, we must write the format for the months in mm/dd/yyyy.

The month is represented from 01-12 in the format. Thus, the first digit of the month can either be 0 or 1, so we write 0[1-9] for the months starting from 01-09. The 1-9 inside the bracket denotes the number written after 0.

Similarly, we can only write 10, 11, and 12 for the month, starting with 1. Therefore, we have written 012 inside the brackets for 1 as 1[012].

We use the | symbol to achieve logical OR. So we use it to separate the rules as (0[1-9]|1[012]).

Finally, we enclose the pattern with the parenthesis to indicate the end of a section.

Next, we have to set the rule for the separators in the format mm/dd/yyyy. The above pattern allows the separators of the symbols -, /, and ..

Now, we write the pattern for days. Days are 1-30, so the days 01-09 can be written as 0[1-9], 10-29 can be written as 12[0-9], and the days 30-31 can be written as 3[01].

We use the same rule for separators after the days in the format mm/dd/yyyy.

We have written 19|20 as the first two digits for the year, meaning the year can only start from 19 and 20. We used the pattern \d\d after 19|20 to denote any two digits coming after 19 or 20.

This is how the above regular expression works. We can now check some test date formats with the regular expression above.

For that, create a variable $reg and store the regex we wrote above in it. Next, create an array $dates and store these random date formats.

['03/22/2021', '22/22/2021', '03\22\2021', '03-22-2021', '03/22/1865']

Then, use the foreach loop to loop the $dates variable as $date. Compare the $date variable with the $reg variable using the preg_match() function.

By doing this, we can check if the test inputs comply with the regex pattern of the mm/dd/yyyy format.

Example Code:

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

$dates =['03/22/2021', '22/22/2021', '03\22\2021', '03-22-2021', '03/22/1865'] ;
foreach($dates as $date){
 if(!preg_match($reg, $date)) {
 echo 'the date format '.$date.' is incorrect'."<br>"; 
 } else{
 echo 'the date format '.$date.' is correct'."<br>";
 } 
}

Output:

the date format 03/22/2021 is correct
the date format 22/22/2021 is incorrect
the date format 03\22\2021 is incorrect
the date format 03-22-2021 is correct
the date format 03/22/1865 is incorrect

Here, the first format follows all the rules. The second format is incorrect because the month cannot be 22.

The third one is incorrect because we have not specified the \ separator. We have specified the - separator, so the fourth date is correct. The year is the 1800s in the last one, so it is incorrect.

Subodh Poudel avatar Subodh Poudel avatar

Subodh is a proactive software engineer, specialized in fintech industry and a writer who loves to express his software development learnings and set of skills through blogs and articles.

LinkedIn

Related Article - PHP Date

Related Article - PHP Regex