I'm trying to access the format of a given date value. I need to check whether the format is in dd/mm/yy
or dd/mm/yyyy
. I have used the following patterns with preg_match()
, but they do not work.
'^\d{1,2}\/\d{1,2}\/\d{4}^' // dd/mm/yyyy
'^\d{1,2}\/\d{1,2}\/\d{2}^' // dd/mm/yy
Can someone help me to find the correct pattern?
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 like10/10/1111
/^\d{1,2}\/\d{1,2}\/\d{2}$/
for strings like10/10/11
You have an error in the syntax of your regular expression. In a regex, the start of the pattern is denoted by ^ while the end is by $.
You can use the following to validate the given date format.
$date_to_check = "22/06/2017";
$true = preg_match("/([0-9]{2})\/([0-9]{2})\/([0-9]{4})/", $date_to_check);
var_dump($true);
However, you shouldn't be validating a date against a regex expression only. You need to pass the Year, month, date to the php's checkdate() function to really validate it.