Jump to content

Archived

This topic is now archived and is closed to further replies.

jtravis

Check User Inputed String for valid date

Recommended Posts

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

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
[!--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.. [=

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
[!--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.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.