homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

checkdate() error

 2:55 pm on Aug 1, 2014 (gmt 0)

Error = checkdate() expects parameter 2 to be long, string given in..

I understand that checkdate() is being passed a string, but I can't see from where.

The code is :-

//Get start dates
$start_date =(from form ($_POST['from'])after checks and clean up); <= 02-08-2014
$sd = substr($from, 0,2); <= 02
$sm = substr($from, 3,2); <= 08
$sy = substr($from, 6,4); <= 2014

$start_date_error = 0;
if ((!checkdate($sm,$sd,$sy))){ $start_date_error =1; ........

If I echo $sm.$sd.$sy it returns 08022014

The code works fine its just I don't like errors and my server log is filling fast.

Is it because I'm using substr? is there an equivalent for integers I should be using?




 3:29 pm on Aug 1, 2014 (gmt 0)

Perhaps because 02 is a string and not an integer, try 12122014 and see if it gives any error. You can try to cast it to an int before passing to checkdate.


 8:18 pm on Aug 1, 2014 (gmt 0)

substr() certainly returns a string, so that is where your string is coming from. However, this should not normally be producing an "error" (actually an E_WARNING).

Normally, if you pass a numeric string to the checkdate() function (as with any function that is expecting a number) then PHP will implicitly convert the string to a number. However, if the string is not numeric then you get this warning.

If the date parts "look" OK then I would hazard a guess that maybe you have a character encoding issue (multi-byte strings)?

As bhukkel suggests, explicitly casting the value to an int before passing it to checkdate() should remove the warning, however, you need to make sure that the value is being converted correctly and not simply lost (if the string cannot be converted then 0 (zero) is returned and your checkdate() call will simply fail with no warning).

$sd = (int)substr($from, 0,2); 
var_dump($sd); // Is this the value you expect? ie. (int)2


 8:19 pm on Aug 3, 2014 (gmt 0)

Hi thanks for the reply. It was a warming not an error.

I have added (int) to all sub parts of both start date and end date and it does give the correct value, hopefully this will stop the warning.

So is there an integer equivalent to substr?


 9:44 pm on Aug 3, 2014 (gmt 0)

So is there an integer equivalent to substr?

You are extracting a substring from a larger string, so there is no "integer equivalent" to this action, other than manually doing the conversion, like you have done.

However, as mentioned above, it is not just the fact that these are strings that is the problem. There would seem to be something non-numeric about them that the function is unable to deal with quietly - although it would seem to be giving the expected result? (But manually converting these to integers prior to calling the function would seem to be solving this... since manually casting the string will never throw a warning, even if the string is entirely non-numeric.)

For instance, try the following "hardcoded" example:

$from = '02-08-2014'; // Hardcoded date string (expected result of processing) 
$sd = substr($from, 0,2); // (string)'02'
$sm = substr($from, 3,2); // (string)'08'
$sy = substr($from, 6,4); // (string)'2014'
echo (checkdate($sm,$sd,$sy) ? 'OK' : 'BAD');

Although strings are being passed to the checkdate() function above, this should not result in a warning message.

(I realise it's not a working example, but you used $start_date in your calculation, but $from in the substr() function call?)


 10:03 pm on Aug 3, 2014 (gmt 0)

You could just remove the leading zeros with trim and a character_mask or if you really like it you could write a regex.


 10:36 pm on Aug 3, 2014 (gmt 0)

remove the leading zeros with trim

trim() still returns a string, and leading zeros are not a problem with numeric strings in PHP. For instance, ('02' == 2) evaluates to true (the string is implicitly converted to an integer).

I suspect a regex might work, and resolve the issue, if you are only looking for numeric digits. However a regex would ordinarily be overkill for something like this. A simpler/alternative way to break the string up, would be to explode() it on the hyphen, although this wouldn't necessarily resolve the above issue.

What character encoding is being used to submit the form?

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved