/    Sign up×
Community /Pin to ProfileBookmark

Date validationand Booleans

I have a form with a box for the user to enter the date in the format dd/mm/yyyy. The latter appears in the box by default. I have a flag called $valid which is only true if the user replaces the default with numbers. The code I use works ok giving the day of the week also, but I’m sure from my knowledge of other languages that there is a more elegant solution. The flag is also required at a later stage.

[code=php]
$yyyy=substr($date,-4);
$mm=substr($date, 3, 2);
$dd=substr($date, 0, 2);

if (is_numeric($dd) and is_numeric($mm) and is_numeric($yyyy)) {
$valid=true ;
} else {
$valid=false ;
}
if ($valid) {
$date=date (“l d-M-Y”, mktime (0,0,0,$mm,$dd,$yyyy)); // formats & o/ps date
}[/code]

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@pyroOct 29.2003 — This, perhaps?

[code=php]<?PHP
$date = "10/29/2003";
list ($month, $day, $year) = preg_split("/[\/-]/", $date);
$valid = false;
if (is_numeric($day) && is_numeric($month) && is_numeric($year)) { #could do a better validation with regexp, if you wanted to
$valid=true;
}
if ($valid) {
$date= date ("l d-M-Y", mktime (0,0,0,$month,$day,$year)); // formats & o/ps date
}
echo $date;
?>[/code]
Copy linkTweet thisAlerts:
@gizmoauthorOct 29.2003 — Looks good to me. Not sure I understand the line starting with list, but will consult the manual. Thanks, Pyro your a pal?
Copy linkTweet thisAlerts:
@pyroOct 29.2003 — Sure thing. And once you look at the manual, you'll know exactly what it does:

[i]http://us2.php.net/manual/en/function.list.php[/i]

[b]list -- Assign variables as if they were an array[/b][/quote]
×

Success!

Help @gizmo spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.29,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...