jtravis Posted March 14, 2006 Share Posted March 14, 2006 Hello all,I'm needing a function or at least a good idea on how to check the value of a string to see if it is in a valid date format before inserting into mysql. I've been looking at the string functions, but if there is something easier to use, I'd rather use that.Form to take input.[code]<td><p align="center"><strong>Date Issued<br> </strong><span class="style3">(Enter '00' for the day if unknown.)</span> </p></td> <td> <input name="dateIssued" type="text" maxlength="10"> <br> <span class="style3">(YYYY-MM-DD)</span><br></td>[/code]Is my best option to loop through the the above string value?TYIA Quote Link to comment https://forums.phpfreaks.com/topic/4956-check-user-inputed-string-for-valid-date/ Share on other sites More sharing options...
keeB Posted March 14, 2006 Share Posted March 14, 2006 preg_match using regular expressions.. or a JScript (also regular expressions) solution would be your best bet.. i will write something out soon to help out [=[code]<?php $date = "2006-03-12"; $pattern = "([0-9]{4})-([0-9]{1,2})-([0-9]{1,2})"; // 4 characters 0-9, followed only by a hyphen, 1 or 2 characters allowed, followed by a hyphen, 1 or 2 characters allowed, followed by a hyphen. if ( preg_match ( $date, $pattern ) ) { //do whatever you want here.. } else { //thrown an error or something }?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/4956-check-user-inputed-string-for-valid-date/#findComment-17450 Share on other sites More sharing options...
jtravis Posted March 14, 2006 Author Share Posted March 14, 2006 Sweet!. Thanks keeB! BTW, I've implemented your idea for debugging. Quote Link to comment https://forums.phpfreaks.com/topic/4956-check-user-inputed-string-for-valid-date/#findComment-17456 Share on other sites More sharing options...
keeB Posted March 14, 2006 Share Posted March 14, 2006 [!--quoteo(post=354982:date=Mar 14 2006, 06:26 PM:name=jtravis)--][div class=\'quotetop\']QUOTE(jtravis @ Mar 14 2006, 06:26 PM) [snapback]354982[/snapback][/div][div class=\'quotemain\'][!--quotec--]Sweet!. Thanks keeB! BTW, I've implemented your idea for debugging.[/quote]Good to hear.. it's saved my ass a few times, that's for sure..It's a little tricky to do in objects, because they have to be defined locally.. [= Quote Link to comment https://forums.phpfreaks.com/topic/4956-check-user-inputed-string-for-valid-date/#findComment-17463 Share on other sites More sharing options...
Barand Posted March 14, 2006 Share Posted March 14, 2006 This will check the y,m,d values are OK[code]$date = "2006-04-31";list ($y,$m,$d) = explode('-', $date);if (checkdate($m,$d,$y)) echo 'OK';else echo "No";[/code] Quote Link to comment https://forums.phpfreaks.com/topic/4956-check-user-inputed-string-for-valid-date/#findComment-17495 Share on other sites More sharing options...
keeB Posted March 14, 2006 Share Posted March 14, 2006 [!--quoteo(post=355022:date=Mar 14 2006, 07:57 PM:name=Barand)--][div class=\'quotetop\']QUOTE(Barand @ Mar 14 2006, 07:57 PM) [snapback]355022[/snapback][/div][div class=\'quotemain\'][!--quotec--]This will check the y,m,d values are OK[code]$date = "2006-04-31";list ($y,$m,$d) = explode('-', $date);if (checkdate($m,$d,$y)) echo 'OK';else echo "No";[/code][/quote]You and your list() function!! It's really cool, and I haven't used it yet.With the preg_match entered up there I don't think it's possible to enter an 'invalid' date, but this is cool. Quote Link to comment https://forums.phpfreaks.com/topic/4956-check-user-inputed-string-for-valid-date/#findComment-17512 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.