Click to See Complete Forum and Search --> : Failed number compares


Ebola
02-18-2007, 12:47 AM
if(($ExpDate >= $From) && ($ExpDate <= $To)){

From: 20060308 | To: 20060312 | ExpDate: 20060310

This if statement above was giving me false until I figured out that I had to multiply From, To, and ExpDate by 1 in order to force them to be ints, but shouldn't it have given me true from the beginning even if it was a string?

This is more a question on how php deals with strings/ints moreso than help, because I no longer have the problem.

If it helps I formed the From/To variables like this:
$From = $Year . $Month . $Day;

NogDog
02-18-2007, 01:05 AM
This works OK for me (PHP 5.2.0):

$From = '2006'.'03'.'08';
$To = '2006'.'03'.'12';
$ExpDate = '2006'.'03'.'10';
if(($ExpDate >= $From) && ($ExpDate <= $To)){
echo "OK";
}
else {
echo "Nope";
}

NightShift58
02-18-2007, 05:57 AM
... or also...
$From = strtotime('20060308');
$To = strtotime('20060312');
$ExpDate = strtotime('20060310');
if ($ExpDate) >= $From && $ExpDate <= $To) {
echo "OK";
} else {
echo "KO";
}...to stay within the original type.

Ebola
02-19-2007, 02:02 AM
I have Version 5.0.4, maybe that was patched up in the newer version?

I came across another weird thing soon after that as well, I was using substr's to get at the year/month/day in the lastlogin variable like so:

$Year = substr($LastLogin, 0, 4);
$Month = substr($LastLogin, 4, 6);
$Day = substr($LastLogin, 6, 8);

The $Month variable was giving me 0218 ($LastLogin value = 20060218) and even if I made the values 3,5 or 5,7 it would give me the values all the way to the end of the variable. I was able to get what I needed by using negative placement like so:

$Month = substr($LastLogin, -4, -2);

And that's been working fine for me since. Has anyone ever come across something like that before?

NogDog
02-19-2007, 03:14 PM
The third arg to substr() is the number of characters to select, not the character number at which to stop. So month would be something like:

substr($lastLogin, 4, 2)