Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Date Regex?

Status
Not open for further replies.

youradds

Programmer
Jun 27, 2001
817
GB
Hi, I've been working on this for 2 days now...and am still not really any nearer to where i was at the beginning. Basically I need to verify that $set_date is equal to dd/mm/yyyy (obviously digits). Is there any PHP function that will do this, or can someone help me with the regex?

Any help would be appreciated :

Thanks

Andy
 
I dunno anything about regexes and it angers me that everyone in the world can understand them but me. They seem like such a good thing! I guess they are the kind of thing you have to learn in a classroom or from someone else... I've hit about every tutorial in the web and I still couldn't write a regex if my life depended on it!

I would just use this:
Code:
<?
function goodDate($some_date)
{
 if(strlen($some_date)==10)
  {
   $some_date = str_replace(&quot;/&quot;,&quot;&quot;,$some_date);
   if(strlen($some_date)==8 && is_numeric($some_date))
    {
     return 1;
    }
  }
 return 0;
}
?>

This will return true if your date is good.

So to test a date you would say:
Code:
$my_b_day = &quot;19/05/1984&quot;;
if(goodDate($my_b_day))
 {
  echo &quot;$my_b_day is valid!&quot;;
 }

Good luck! -gerrygerry
Go To
 
how are you creating $set_date? user input? if so can you not use drop-boxes for the input as this forces correctness?

Someone posted a nice script here for creating these recently.

If you want to validate what is in mySQL or another DB, post again to clarify some more :)
***************************************
Party on, dudes!
 
I have a nice javascript/PHP combo box solution that will only let the user select dates that exist in the DB. First, it queries the DB for all the dates, then it builds a series of javascript arrays. The user's select box choice are then limited to select boxes built from these array. This script not only generates select box options, but through a little dynamic sleect box updating, there is no way to select a date that does not exist in the results of the DB query. When the user selects a year, the month select box is populated with values for all the months that &quot;exist in that year&quot;. When the user selects the month (after slecting the year), the day slect box is dynamically modified to only contains days that were retuned as existing for that year and month combination. Confused yet? Just give it a try! Note: my solution is IE only, I couldn't get anyone to help me with a NS port.

Check it out:
======================================
Include these in the page...
<?
require(&quot;js_arrays.php&quot;);
require(&quot;menu_script.php&quot;);
require(&quot;menu.php&quot;);
?>
======================================
Code:
[js_arrays.php]
Code:
<?
echo &quot;<script>&quot;;

 @MYSQL_CONNECT(&quot;$db_host&quot;,&quot;$db_user&quot;,&quot;$db_pass&quot;);
 @MYSQL_SELECT_DB(&quot;$db_name&quot;);
 $query = &quot;SELECT DISTINCT date FROM $table_name ORDER BY date DESC&quot;;
 $result = @MYSQL_QUERY($query);

 $sentDates = Array();
 $years_opts = &quot;&quot;;

echo &quot;\n\narrDates1 = new Array();\n&quot;;
 for($counter=0;$date=@MYSQL_RESULT($result,$counter,&quot;date&quot;);$counter++)
  {
   if(!isset($sentDates[substr($date,0,4)]))
    {
     $sentDates[substr($date,0,4)] = Array();
     echo &quot;\n arrDates1[&quot;.substr($date,0,4).&quot;] = new Array();&quot;;
    }
   if(!isset($sentDates[substr($date,0,4)][substr($date,5,2)]))
    {
      $sentDates[substr($date,0,4)][substr($date,5,2)] = Array();
      echo &quot;\n\t arrDates1[&quot;.substr($date,0,4).&quot;][&quot;.substr($date,5,2).&quot;] = new Array();&quot;;
    }
   if($sentDates[substr($date,0,4)][substr($date,5,2)][substr($date,8,2)]!=1)
    {
      $sentDates[substr($date,0,4)][substr($date,5,2)][substr($date,8,2)]=1;
      echo &quot;\n\t\t arrDates1[&quot;.substr($date,0,4).&quot;][&quot;.substr($date,5,2).&quot;][&quot;.substr($date,8,2).&quot;] = 1;&quot;;
    }
  }

while(list($year, $value)=each($sentDates))
{
      $year_opts .= &quot;\t\t<option value=&quot;.$year.&quot;>&quot;.$year.&quot;</option>\n&quot;;
}

 MYSQL_CLOSE();
 
echo &quot;\n</script>\n\n&quot;;
?>
======================================
Code:
[menu_script.php]
Code:
<script>
function selectChange1(control, controlToPopulate)
{
  var myEle ;
  var x ;

  for (var q=controlToPopulate.options.length;q>=0;q--) controlToPopulate.options[q]=null;
  if (control.name == &quot;YEAR&quot;)
   {
    for (var q=date_menu.DAY.options.length;q>=1;q--) date_menu.DAY.options[q] = null;
   }

  myEle = document.createElement(&quot;option&quot;) ;
  myEle.value = 0;
   if(control.name == &quot;YEAR&quot;){myEle.text = &quot;MONTH&quot; ;}
   else if(control.name == &quot;MONTH&quot;){myEle.text = &quot;DAY&quot; ;}
  controlToPopulate.add(myEle);

if(control.name==&quot;YEAR&quot;)
 {
 if(date_menu.YEAR.value != 0)
  {
  for(var q=0; q<=(arrDates1[date_menu.YEAR.value].length); q++)
  {
   if(arrDates1[date_menu.YEAR.value][q])
    {
     myEle = document.createElement(&quot;option&quot;) ;
     myEle.value = myEle.text = q;
     if(q<10){myEle.text = &quot;0&quot;+q;}
     date_menu.MONTH.add(myEle) ;
    }
   }
  }
 }
 
else if(control.name==&quot;MONTH&quot;)
 {
 if(date_menu.MONTH.value != 0)
 {
 for(var q=0; q<=arrDates1[date_menu.YEAR.value][date_menu.MONTH.value].length; q++)
  {
  if(arrDates1[date_menu.YEAR.value][date_menu.MONTH.value][q])
   {
    myEle = document.createElement(&quot;option&quot;) ;
    myEle.value = myEle.text = q;
    if(q<10){myEle.text = &quot;0&quot;+q;}
    date_menu.DAY.add(myEle);
   }
  }
 }
 }
}
</script>
======================================
Code:
[menu.php]
Code:
<hr>
<b>Existing Entries in Database:</b>
<table border=0>
       <tr>
           <td>
           <select name=&quot;YEAR&quot; onchange=&quot;selectChange1(this, date_menu.MONTH);&quot;>
		<option value=0>YEAR</option>
<?
        echo $year_opts;
?>
           </select>
           </td>
           <td>
           <select name=&quot;MONTH&quot; onchange=&quot;selectChange1(this, date_menu.DAY);&quot;>
		<option value=0>MONTH</option>
           </select>
           </td>
           <td>
           <select name=&quot;DAY&quot;>
   		<option value=0>DAY</option>
            </select>
           </td>
           <td>
           <input type=&quot;submit&quot;>
           </td>
       </tr>
</table>
</form>
<hr>

I guess this might nor even be relevant to what you wanted but since karver mentioned a cool date selection script, I had to step forward ;-)

Again,. this is IE only which i s'pose is ok for me since I'm on an intranet. Cross browser is always better though... someone wanna work on this?

Hope this might have helped! -gerrygerry
Go To
 
Thanks gerrygerry, your code worked great. I made a few modifications to it, to verify further if the date was valied. I'm now using;

<?
function goodDate($some_date)
{
if(strlen($some_date)==10)
{
$some_date_new = str_replace(&quot;/&quot;,&quot;&quot;,$some_date);

$cut = explode(&quot;/&quot;, $some_date);
if ($cut[0] <= 31 && > 0) { error(&quot;Days format is incorrect! Invalied. Please try again.&quot;); }
if ($cut[1] <= 12 && > 0) { error(&quot;Months format is incorrect! Invalied. Please try again.&quot;); }
if ($cut[2] <= 2100 && > 2001) { error(&quot;Yeah format is to high! Invalied. Please try again.&quot;); }
if(strlen($some_date_new)==8 && is_numeric($some_date_new))
{
return 1;
}
}
return 0;
}
?>

Works like a charm :)

Thanks again

Andy
 
i'm assuming that you want correct dates, right? like 02/31/2002 is incorrect, but 02/26/2002 is correct...

try this:

/*
*isValidDate -
@params: $dt, mm/dd/yyyy date
@returns: boolean, true if date is valid, false otherwise
checks any mm/dd/yyyy format and returns it's validity
*/
function isValidDate($dt) {
$arr = explode(&quot;/&quot;,$dt);
if(count($arr) != 3) {
return(false);
} //end if

return(checkdate($arr[0],$arr[1],$arr[2]);
} //end fxn

see:

it's basically a wrapper for the checkdate function. If you want to add any more functionality, it's easy, just change the if statement.

ex:
if(count($arr) != 3 || $arr[2] < 1975)

now it only validates dates > 01/01/1975

I threw this together at work (ie - no php) so I haven't gotten a chance to test it. You should get the idea though...

hth leo

------------
Leo Mendoza
lmendoza@garbersoft.net
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top