Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Weird string problem

Status
Not open for further replies.

999Dom999

Technical User
Apr 25, 2002
266
0
0
GB
I'm creating a query its long so I won't post it all but this part in question.

Code:
$first = date('Y-m-d', mktime(0, 0, 0, 10, 1, $y)); 
$last = date('Y-m-t', mktime(0, 0, 0, 10, 1, $y)); 
$dateQuery = " ITRAN.IT_DUE >= { d '$first'} AND ITRAN.IT_DUE <= { d '$last'} AND ";

So $dateQuery outputs
Code:
ITRAN.IT_DUE >= { d '2012-10-01'} AND ITRAN.IT_DUE <= { d '2012-10-31'} AND
When I send the full query with date range I get spurious results, I'm expecting no results at all, but I get many.

Now the weird bit if I echo $dateQuery then copy and paste the output into the query it works as expected. Or if I don't use the date function and do $first = '2012-10-01'; $last = '2012-10-31'; then it also works as expected.

So somehow the date function although its string is somehow different to $first = '2012-10-01';

If I do echo gettype($first); it outputs a string.

I tried:
Code:
$first = date('Y-m-d', mktime(0, 0, 0, $i, 1, $y)); 
$first = (string)$first;
//and
$first = date('Y-m-d', mktime(0, 0, 0, $i, 1, $y)); 
$first = print_r($first,true); 
//and
$first = date('Y-m-d', mktime(0, 0, 0, $i, 1, $y)); 
$pieces = explode("-", $first); //was a stab in the dark
$first = $pieces[0]."-".$pieces[1]."-".$pieces[2];
//and
$first = date('Y-m-d', mktime(0, 0, 0, $i, 1, $y)); 
$first = strval($first); 
//and
var_dump($first); //tells me its a string.

None Of the above works. Its driving me crazy. If I copy and paste the output it works fine, any ideas why this would be anything other than a normal string. I use the date function all the time without any problems in my queries so not sure why this is happening?


 
AHHHHH!!!!!

Had nothing to do with the string.

I was running a loop with an array holding database values, when it returned no results the array still held the info from the previous result set. If I hard coded the date I would always get the result I expected!

D'oh so silly!!!

That mind bender is over! :)
 
you have not said what database engine you are using.

the date() function returns a string, so casting to a string is unnecessary. something else is causing the issue. give us more information.
 
Thanks as my post above I figured it out, it was my mistake [sadeyes]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top