• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 185
  • Last Modified:

Format Check

Hi.

I am writing a PHP script that I need to check to see if the passed date matches the format DD/MM/YY.

So it should check that the string is:

Number between 1-31 / Number between 1-12 / Number between 0-99

I need it to just return true if it is ok, false if not.

Is this possible, and how do I do it?
0
gileze33
Asked:
gileze33
  • 2
  • 2
1 Solution
 
RoonaanCommented:
Here you go:

function isDDMMYY($string) {
  $expression = '#^(0[1-9]|1[0-9]|3[01])/(0[1-9]|1[012])/[0-9]{2}$#';
  return preg_match($expression, $string);
}

-r-
0
 
gileze33Author Commented:
Hi.

I have used your code to make the function:

function dfc($string) {
      $expression = '#^(0[1-9]|1[0-9]|3[01])/(0[1-9]|1[012])/[0-9]{2}$#';
      return preg_match($expression, $string);
}

I then pass it

if(dfc("17/5/02")==false){ print "<span class='error'>Incorrectly formatted date!</span><br>"; }

This says that the date is incorrectly formatted, which can't be right, because that date is correctly formatted.
0
 
RoonaanCommented:
My fault,

I set the MM to require two digets always rather than allow 1 or 2.

Use this:

function dfc($string) {
     $expression = '#^(0[1-9]|1[0-9]|3[01])/(0?[1-9]|1[012])/[0-9]{2}$#';
     return preg_match($expression, $string);
}

Kind regards

-r-
0
 
gileze33Author Commented:
Don't apologise, was my fault, I didn't even keep to my own formatting requirements!

Sorry!

Thanks for all your help!
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now