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 formatting

Status
Not open for further replies.

alifyag

MIS
Apr 15, 2002
18
US
hello,
i have a problem with date formatting.
i am using PHP and mysql.
i want to accept the date from the user in mm/dd/yyyy format and if the user enters anything else, i wont except it.
how do i set the criteria on my textbox where the date is inputted.
thanks

 
If you want to do it on the server side (i.e., the user submits the form, and if there is an error, the server presents the form page again with error messages) something like:

if (preg_match ('/^\d{2}\/\d\{2}\/\d{4}$/', $_POST['<input name>']) != 0)
{
if (strtotime($_POST['<input name>']) === -1)
{
// It's a good date
}
}

Should do it. If you want to do it on the client side, then the question is better asked in the JavaScript forum ______________________________________________________________________
TANSTAAFL!
 
Yes, you can use Regular expressions to handle date format input, but that is like using a hammer to squash a bug ;-). PHP has a great little function called sscanf ( ) which parses an input string according to a format, essentially doing the reverse of sprintf.

Sscanf has the added value of not only checking for correct format of your input string, but returning an array of values based on what you want to parse.

Here is a test script to illustrate:

Code:
<html>
<body>

<form method=&quot;POST&quot;>

	<input type=text name=inputdate>

	<input type=submit>

</form>

<?php

if($_POST){

	echo $inputdate . &quot;<br><br>\n\n&quot;;

	//get the values as an array
	$date_in = sscanf($_POST[&quot;inputdate&quot;], &quot;%02d/%02d/%04d&quot;);
	var_dump($date_in); // examine the array

	//nice trick--dereference array as it is being created
  list($month, $day, $year) = sscanf($_POST[&quot;inputdate&quot;], &quot;%02d/%02d/%04d&quot;);

	echo &quot;<br><br>\n\n
	month = $month <br>
	day = $day <br>
	year = $year
	&quot;;

}

?>

</body>
</html>

Just run this PHP script, enter a date in the form, and see how it validates. The format for sscanf used here is %02d for the two short numbers and %04d for the number with (you guessed it) 4 digits, such as the year.

Now, after you receive these values from the input string, you can easily use more PHP programming to check for other conditions you might want to check for in each of the returned values, such as
Code:
if($year < 1970)
or some such.

It takes a little getting used to but once you learn the sscanf and sprintf rules, you are borrowing heavily from the C programming book, and your geek buddies might even start calling you a guru [thumbsup2]. -------------------------------------------

Big Brother: &quot;War is Peace&quot; -- Big Business: &quot;Trust is Suspicion&quot;
(
 
or, you could just simplify things using a sleect box for the month and day, then make them type in a year... there are also numerous javascripts to aid you... check out this script: -it accounts for the different number of days in a month and leap year... this way, the user cant give it to you in the incorrect format :-D -gerrygerry

Standard response to one of my posts:
&quot;No where in your entire rantings did you come anywhere close to what can be considered a rational answer. We are all now dumber from having heard that. I award you no points and may God have mercy on your soul.&quot;
 
Oh, I agree that those Javascript widgets are nice on the client side (I use them all the time), but they don't guarantee anything on the server side. People can bypass that stuff quite easily and even unintentionally sometimes. Even specifying separate form fields doesn't guarantee anything in the end, because someone's browser could have a glitch and send the wrong data through, (not to mention someone creating their own form, or forging a POST with some script, etc...) -------------------------------------------

Big Brother: &quot;War is Peace&quot; -- Big Business: &quot;Trust is Suspicion&quot;
(
 
that's a good point rycamor... nothing is 100% ;-) -gerrygerry

Standard response to one of my posts:
&quot;No where in your entire rantings did you come anywhere close to what can be considered a rational answer. We are all now dumber from having heard that. I award you no points and may God have mercy on your soul.&quot;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top