Welcome to WebmasterWorld Guest from 54.160.163.163

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

checkdate() error

     

Pollos

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

10+ Year Member



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?

Thanx

bhukkel

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

5+ Year Member



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.

penders

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

WebmasterWorld Senior Member penders is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month



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

Pollos

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

10+ Year Member



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?

penders

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

WebmasterWorld Senior Member penders is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month



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?)

bhukkel

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

5+ Year Member



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

penders

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

WebmasterWorld Senior Member penders is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month



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?
 

Featured Threads

Hot Threads This Week

Hot Threads This Month