0

I'm sure this is something stupid that I'm not seeing, but I have a variable updating when I feel like it shouldn't be.

$dateholder = "";
$currentdate = "";
$rsresults->MoveFirst();
while (!$rsresults->EOF) {
    $currentdate = $rsresults->fields['newdate'];
    echo "current date: " . $currentdate . "<br>";
    echo "previous date: " . $dateholder . "<br>";
    if($currentdate > $dateholder){
        echo "CURRENT DATE DIFFERENT THAN DATEHOLDER<br><br>";
        $dateholder = $currentdate;
    }
    $rsresults->MoveNext();
}

I'm getting a date from my database. If it's different than the previous date, I want to output DIFFERENT DATE. (it's ordered by date). Here are the results I'm getting:

current date: 2018-01-22
previous date:
CURRENT DATE DIFFERENT THAN DATEHOLDER

current date: 2018-01-22
previous date: 2018-01-22
current date: 2018-01-23
previous date: 2018-01-23
current date: 2018-01-23
previous date: 2018-01-23
current date: 2018-01-24
previous date: 2018-01-24
current date: 2018-01-24
previous date: 2018-01-24
current date: 2018-01-25
previous date: 2018-01-25

The first one is expected, but how is my dateholder variable getting updated without ever echoing out CURRENT DATE DIFFERENT?

chris85
  • 23,846
  • 7
  • 34
  • 51
Edward Glasser
  • 183
  • 4
  • 17

2 Answers2

0

EDIT I

Regarding the problem:

In the code what is returned when assigning the values is not a String but an Object (confirmed in comments).

So when the $dateholder = $currentdate assignment is done, the $dateholder references the same object than $currentdate instead of just taking the current string value.

After that any change in $currentdate also changes $datholder because they point to the same object.

To solve the probem:

Cast to (string) before doing the assignment like explained here SQL return “variant Object” how to convert to regular data

In your case:

$dateholder = "";
$currentdate = "";
$rsresults->MoveFirst();
while (!$rsresults->EOF) {
    $currentdate = (string) $rsresults->fields['newdate'];
    echo "current date: " . $currentdate . "<br>";
    echo "previous date: " . $dateholder . "<br>";
    if($currentdate > $dateholder){
        echo "CURRENT DATE DIFFERENT THAN DATEHOLDER<br><br>";
        $dateholder = (string) $currentdate;
    }
    $rsresults->MoveNext();
}
Juan
  • 5,525
  • 2
  • 15
  • 26
  • I ended up doing something similar, but using the strtotime() function. Thank you. edit: you were correct about it having to do with the type variable and it creating a pointer to the variable rather than updating the variable, which was the real answer to my question of why part of the if statement was "running", but not the echo. – Edward Glasser Mar 28 '18 at 15:26
  • @AbraCadaver Just saying it is not correct doesn't help much – Juan Mar 28 '18 at 15:38
  • Objects are assigned by reference not the value of their properties: https://3v4l.org/XYdqZ – AbraCadaver Mar 28 '18 at 15:41
  • @AbraCadaver And how is that different from what I said? When $dateholder = $currentdate happens, $dateholder references the same object than $currentdate, so any change in $currentdate is also 'reflected' in $dateholder. – Juan Mar 28 '18 at 15:45
  • _In the code what is returned when assigning the values is not a String but an Object. So when the $dateholder = $currentdate assignment is done, the $dateholder references the same object than $currentdate instead of just taking the current string value. After that any change in $currentdate also changes $datholder because they point to the same object._ `=== FALSE` – AbraCadaver Mar 28 '18 at 15:47
  • @AbraCadaver I am really sorry but I don't get the point. I'll clarify the EDIT I is regarding the OP's original code. Not sure if it is that what is confusing. – Juan Mar 28 '18 at 15:54
  • What you have said is false, it doesn't work that way. Not sure how much clearer I can make it. – AbraCadaver Mar 28 '18 at 17:12
  • @AbraCadaver You are not a newby, If you really wanted to make it clear you could provide an answer, and in that answer it should be cristal clear why my answer is wrong. In that way the OP and anyone else reading this question including myself could benefit of learining something. – Juan Mar 28 '18 at 17:24
0

you're comparing strings not dates just like this

if ( "hi" > "hello" ){
 echo "hello";
}

to compare dates you need to convert the strings to dates then compare them

$date1 = strtotime('2018-01-22');
$date2 = strtotime('2018-02-22');

if($date1 < $date2){
  //do somthing 
}
zakaria35
  • 857
  • 1
  • 7
  • 12
  • yea, it's not that I was necessarily wanting a solution. I was wondering why the variable was getting updated in the if statement, but the echo, in the same if statement, wasn't running. part of the if statement was happening, and not the other part. edit: it had to have something to do with pointers as juan mentioned. – Edward Glasser Mar 28 '18 at 15:27