Methods for making sure date is entered in correct format

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Methods for making sure date is entered in correct format

Post by JayBird »

A problem i always have is making sure people enter dates into forms int he correct format.

Say i wanted all dates to be ntered like 05-08-2004 i have a number of options

Option 1
Let the user enter anything they want, then use REGEX to check it. If it is wrong, spit out an error.

Good thing about this is that it is less time consuming for the user to fill in the dates as there are going to be quite a few date fields on the forms i am using

Option 2
Use select boxes in the form, so the user can only select options that are correct, not allowing them to enter incorect data. Making the user select the day number, the month number and the year from a drop down can be time consuming and laborious when there are a few date fields to be completed.

What other options could there be? What method would you go for?

Mark
User avatar
Bill H
DevNet Resident
Posts: 1136
Joined: Sat Jun 01, 2002 10:16 am
Location: San Diego CA
Contact:

Post by Bill H »

I use option 2 but I "preset" the dates whenever possible so that, more often than not,
all they have to change (if anything) is the day.

Maybe you can't always make the necessary assumptions, but...
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post by CoderGoblin »

1) if using a database you cast try to cast the date using a select and see if a result is returned.
2) Use the php strtotime function
http://www.php.net/manual/en/function.strtotime.php

Example of use ripped from the page...

Code: Select all

<?php
$str = 'Not Good';
if (($timestamp = strtotime($str)) === -1) {
   echo "The string ($str) is bogus";
} else {
   echo "$str == " . date('l dS of F Y h:i:s A', $timestamp);
}
?>
Advantage user can put in additional information such as

Code: Select all

<?php
echo strtotime("now"), "\n";
echo strtotime("10 September 2000"), "\n";
echo strtotime("+1 day"), "\n";
echo strtotime("+1 week"), "\n";
echo strtotime("+1 week 2 days 4 hours 2 seconds"), "\n";
echo strtotime("next Thursday"), "\n";
echo strtotime("last Monday"), "\n";
?>
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

CoderGoblin: That seems like a pretty good method. Im gonna implement it and see how it goes.

Cheerz

Mark
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

I'd use the pulldown menus. It requires on less page load. Alternatively, you could pop up a calendar and let them just click . I use http://javascript.internet.com/calendar ... icker.html often
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply