tags:

views:

29

answers:

3

I have been working with the checkdate function within PHP. You can see the code that I have used at http://codepad.org/JY4hG7jo feel free to fork the code where you see fit.

Basically, if I check a month with a leading zero for the months 08, 09 I get an invalid date response. However, when I run it through a loop to check all the months; It returns a valid response for these months.

The main issue is not why the loop produces different results, more why the date is classed as invalid. The loop is likely an error on my part.

Any insight is appreciated.

+2  A: 
var_dump(checkdate(08, 1, 2007));
var_dump(checkdate(08, 01, 2007));
var_dump(checkdate(09, 1, 2007));
var_dump(checkdate(09, 01, 2007));

The values are considered as octal due to the leading 0, try '09' or just 9. Should work.

Alix Axel
Thats great, thans for the tip. The inital code actaully came from a book that I'm currently reading. The book itself its great and the code provided works fine apart from 08/09 values. The initial code is here: http://codepad.org/h67Rl8AR is there any way that I can cast the input first as a integer. I have tried using both (int) and intval() within the function which failed. I then printed the values that are passed to the function; this shows that the values don't even make it to the functions. Any tips?
Jamie
You can play around with the octdec(), decoct() and intval() functions.
Alix Axel
+1  A: 

If you want to use leading zeros, quote your argument values.

$date->setDate("2008", "08", "01");

simplemotives
+1  A: 

As others have pointed out, you need to provide your arguments as strings (in quotes), not octal numbers. There is no way you can cast them to any other type, as they are already integers - the compiler itself is interpreting them as octal. Specifically, it is trying to interpret them as octal, failing (because in octal there is no such digit as 8, or 9), and populating the variable with 0 instead.

Incidentally, is_numeric is a poor choice of function when testing for integers, since it will allow any kind of numeric value, including things like '-0.5e5', which are obviously no use to you here. The surest check is ctype_digit, but remember that it must be given a string, so you have to do if( ctype_digit( (string)$var ) ) { ... }

IMSoP
That's great! Thanks for the input on is_numeric also!
Jamie