Jump to content
xisto Community
Mordent

Isdate() ?

Recommended Posts

Hey all! Another quick PHP question for you:

Is there a function that can check whether a string you enter is a date in the format of a second string? For instance, if I typed:

$validDate = isDate ( "2009-08-13", "YYYY-MM-DD" );

I would expect to have true stored in $validDate. I can't seem to find one in the PHP manual, nor has a google search presented one. The closest I've been able to find are here and, to some extent here. Both could be tweaked to my needs with relatively little difficulty, I was just curious if a more "built-in" functionality existed.

Share this post


Link to post
Share on other sites

There is nothing that tells PHP that string should be treated as a date, so effectively all you want is to run a regular expression against that string and check whether it matches the format?

There is a function in PHP that will parse a date, but it is for pulling the relevant information into an array rather than just checking it matches the form you give.

Share this post


Link to post
Share on other sites

PHP's strtotime function returns false if it fails to pull a date from the string you give it.  The only problem with that is, strtotime is so flexible, random numbers and characters could actually be translated into a date.

If you want to check whether data is entered in a specific format, the date_parse_from_format function rvalkass mentioned is the way to go.  If the values of "error_count" and "warning_count" in the array it returns are 0, the date is valid.  I haven't used the function before, but I would assume errors are generated if the format is invalid and warnings are generated if, say the entered date was October 42.  The code should look something like this:

$date = parse_date_from_format("2009-08-13", "Y-m-d");$is_valid = ($date['error_count'] + $date['warning-count'] == 0);//Or for a less strict validator://$is_valid = ($date['error_count'] == 0);</P><P>
You can find a list of the special characters you can use in a format string here.

Share this post


Link to post
Share on other sites

There is nothing that tells PHP that string should be treated as a date, so effectively all you want is to run a regular expression against that string and check whether it matches the format?
There is a function in PHP that will parse a date, but it is for pulling the relevant information into an array rather than just checking it matches the form you give.

Finally got around to actually trying to implement this, and I have to say that it worked like an absolute charm. Thanks a bundle!

Share this post


Link to post
Share on other sites

Hmmm...okay, so this is an interesting conundrum for you:

 

Fatal error: Call to undefined function date_parse_from_format() in someaddressthat'sirrelevant on line 13

Yes, I use that function in the place it mentions, and yes, it worked fine when testing it locally. Only when I uploaded it to my Xisto hosting did I get that nasty error. My knowledge of PHP configurations is terrible, is there some flag or somesuch that I need to change to include that function?

 

EDIT: Found a solution to the problem by brushing up on my French and reading this page. About 90% of the way down it says something about PHP versions, and the "date_parse" function.

 

La fonction que j'ai donn? (date_parse_from_format) n'est utilisable qu'? partir de PHP 5.3.

Tu peux toujours utiliser la fonction date_parse si tu n'as pas la possibilit? de changer la versio de PHP, bien que dans ce cas l? solution de b-dav semble plus appropri?e.

Any francophones amongst you feel free to enlighten me as to what it says exactly, but I can get the general idea. Edited by Mordent (see edit history)

Share this post


Link to post
Share on other sites

The function you are trying to use is only valid in versions of PHP equal to or newr that php 5.3.
Cpanel lists the version used by the Trap as version 5.2.5, so the function is not available for use on your Hosting account.

See the http://de2.php.net/date_parse_from_format link to read about the function and see if there is a method for earlier versions of php.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | 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.