After query is run The resulting while loop creates two rows that have the same $dateDiff value when they should be different. Does this have something to do with the DateTime object keeping the same value and not changing even though there is a while statement?


//while loop starts here {

// start date
$start_date = $a1["created"];

// end date
$end_date = date('Y-m-d');

$diff = strtotime($start_date) - strtotime($end_date);

// 1 day = 24 hours
// 24 * 60 * 60 = 86400 seconds
$dateDiff = ceil(abs($diff/86400));

$date = new DateTime($start_date); // Y-m-d
$date->add(new DateInterval('P30D'));

if ($dateDiff < 30) { 
echo $dateDiff;
echo $date->format('m-d-Y');
}

//while loop ends here }

You have not provided enough detail for anyone to answer here, I don' think. It sounds like you say $dateDiff should be different for two different iterations of your while loop, but we don't even know how your while loop is defined, or what values you have for $a1["created"]. You say the value should be different, but you provide no detail about why they should be different.

    crawdidly // 1 day = 24 hours
    // 24 * 60 * 60 = 86400 seconds
    $dateDiff = ceil(abs($diff/86400));

    Protip: not all days are 86400 seconds long.

    This by itself is a red flag. It says "I'm trying to reimplement my own date-handling arithmetic" which is a bad sign when date/time libraries are already provided. PHP has things like the DateInterval class (which can be used to represent the difference between two DateTime objects). Are you trying to compare two different ways of doing something?

    Since we don't know what $a1['created'] contains we've got no idea what $start_date should be, nor $diff nor what it has to do with a 30-day interval; and especially no idea why any of that should be changing in whatever loop this is part of (since that isn't given either).

    What we see:

    1. You get a date in some form from $a1['created'] (the "start date")
    2. You get today's date (the "end date")
    3. You subtract the end date from the start date (the result will be negative unless the start date is after the end date)
    4. Ah, that's why the abs() is there; "throw stuff at the wall until something sticks". Divide that by 86400 and round up and hope that gives the right number of days
    5. Create a DateTime object from $a1 again
    6. Add thirty days. Why thirty? I don't know. How does this relate to the day calculation in step 4? I don't know.
    7. If the number from step 4 is less than 30, print it and the date from step 6 in month-day-year format.

    So ... what does all that achieve and what is it supposed to achieve?

      I had a variable named wrong, I must have looked at it 30 times.

        Write a Reply...