Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I am trying to make a date regex validator. The issue I'm having is that I'm using an input field with "date" type, which works like a charm in Chrome; it opens a calendar-like in Chrome, but in the rest it does nothing, so I decided to go for a manual input of the date for the rest.

This is my error throwing message (I'm looking for YYYY-MM-DD format):

$date_regex ='#^(19|20)\d\d[\- /.](0[1-9]|1[012])[\- /.](0[1-9]|[12][0-9]|3[01])$#';
$hiredate = $_POST['hiredate'];
if (!preg_match($date_regex, $hiredate)){
    $errors[] = 'Your hire date entry does not match the YYYY-MM-DD required format.';
}

I know there are a lot of examples about this, but I tried like 20 already and I couldn't solve it. Maybe I'm missing something.

Here's the input field, if somewhat relevant:

<input type="date" name="hiredate" />
like image 829
Cataneo Avatar asked Nov 04 '13 17:11

Cataneo


People also ask

How do you check if a date is valid in PHP?

PHP | checkdate() Function The checkdate() function is a built-in function in PHP which checks the validity of the date passed in the arguments. It accepts the date in the format mm/dd/yyyy. The function returns a boolean value. It returns true if the date is a valid one, else it returns false.

How do I format a date in regex?

To match a date in mm/dd/yyyy format, rearrange the regular expression to ^(0[1-9]|1[012])[- /.] (0[1-9]|[12][0-9]|3[01])[- /.] (19|20)\d\d$. For dd-mm-yyyy format, use ^(0[1-9]|[12][0-9]|3[01])[- /.]


1 Answers

Do not use regex for this, you can get the same result by using DateTime::createFromFormat

// specify your date's original format, in this example m/d/Y (e.g. 08/31/2013)
$format = "m/d/Y";
$hireDate = DateTime::createFromFormat($format, $_POST['hiredate']);
if(!$hireDate) {
 // createFromFormat returns false if the format is invalid;
} else {
   //change it to any format you want with format() (e.g. 2013-08-31)
   echo $hireDate->format("Y-m-d");
}

you can read more here:

http://php.net/manual/en/datetime.createfromformat.php

However, it seems like the issue is totally unrelated to PHP.

PHP runs on the back end, and it seems like you have a front end problem.

I also doubt the problem is the input type you use. If one browser doesn't support the input type you specified, then it defaults to text. See it here:

http://jsfiddle.net/FKGCA/

My browser doesn't know what the <input type="whatever" /> is, so it defaults the input type to "text". If I wrap those 4 inputs in a <form action="myForm.php" method="POST"></form> tag, the browser sends the inputs to the server because the server doesn't care/know if the inputs were hidden, radio buttons, selects, texts, or password. The servers only receives raw-data.

More than likely, your issue is with your Javascript, and not with your PHP. Try to see if the browser that doesn't display your widget tells you that there's an error of some kind in your page.

Safari and Firefox have development/debugging tools, not so sure about IE.

like image 122
ILikeTacos Avatar answered Sep 19 '22 00:09

ILikeTacos