PHP regex needed for dd/mm/yyyy format

Hi all,

As per the title, I need a regular expression in the dd/mm/yyyy to work with the pregmatch function. I have tried online but not having much joy, I keep getting “unknown modifier errors”.

Any help MUCH appreciate.

Kind regards and thanks

What are you planning on doing with it? Are you using it for validation or are you needing to capture the individual elements?

Hello Anthony, Simply to validate incase the user has javascript turned off.

Kind regards

preg_match("^\\d{1,2}/\\d{2}/\\d{4}^", "datehere");

Made the first part 1 or 2 in case they put 7 for July instead of 07.

If you’re just needing a rather crude implementation with no chekcing of the actual values, you could use…

function isDate($date){
  return 1 === preg_match(

If you’re wanting, what seems to be, the mac daddy of patterns

#dd/MM/yyyy with leap years 100% integrated Valid years : from 1600 to 9999
function isDate($date){
  return 1 === preg_match(

AnthonySterling - Helping people prevent those 17th century goons from registering. :wink:


Ha ha. I spy a new signature! :p[/ot]

Anythony, im sure those regex’s work great but its not working for me. I am finishing of a clients existing validation script, so I did not write it from scratch as I would have prefered.

My client is using this script for validation :-

And the validation function is being called thus: -

$dateRegExp = "^(((0[1-9]|[12]\\d|3[01])\\/(0[13578]|1[02])\\/((19|[2-9]\\d)\\d{2}))|((0[1-9]|[12]\\d|30)\\/(0[13456789]|1[012])\\/((19|[2-9]\\d)\\d{2}))|((0[1-9]|1\\d|2[0-8])\\/02\\/((19|[2-9]\\d)\\d{2}))|(29\\/02\\/((1[6-9]|[2-9]\\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))))$";
	$rules[] = "required,StartDate,Please enter a valid Start date";
    $rules[] = "reg_exp,StartDate, ".$dateRegExp.",i, Please enter your Start Date in the format dd/mm/yyyy"; 
$errors = validateFields($_POST, $rules);

Ive been able to use all kinds of regular expresions with it, but for some reason its not likeing dates…

Could the space after StartDate, be an issue? Try removing it. Your other attempts would probably work then too. :slight_smile:

Oh yeah looks like it :slight_smile: Just one thing, it accepts a single 0 as a date?

It doesn’t this end.

); # bool(false)

); # bool(false)

Maybe the library you’re using is adding another layer of complexity there?

It does on the form however :slight_smile: may I pm you the link to the form possibly to see what I mean?

Highly likely! I prefer to write my own, but I have no choice on this occasion.


The problem lies in the library you’re using.

if (!empty($fields[$field_name]) && !preg_match($reg_exp, $fields[$field_name]))

PHP’s [fphp]empty[/fphp] function considers ‘0’ empty.

The following things are considered to be empty:    
  • “” (an empty string)
  • 0 (0 as an integer)
  • “0” (0 as a string)
  • NULL
  • array() (an empty array)
  • var $var; (a variable declared, but without a value in a class)

Ahhh wonderful ! What is the best way to remedy this? :slight_smile:

Personally, for speed, I’d add a minimum length requirement and be done with it for now. :slight_smile:

Say, 8?

Good idea!!

Didn’t want to hijack, but I’m curious if anyone has any thoughts on how to lock down the simpler one by allowing -./

Except! They have to match, ya know?


That works, except it will allow things like 10/7.1984
So possibilities:

Uniformity! Any ideas?

Off the top of my head, I’d normalise the string to something expected then test that. It would keep the RegExp as focused as possible, or rather, simple as possible. :stuck_out_tongue:


function isDate($date){
  $date = str_replace(array('.', '-', '\\\\'), '/', $date);
  return 1 === preg_match(