Jump to content

Recommended Posts

Thanks in advance for the help.  Below is my code which compares the date a record was created with today's date.  During each access of the table I calculate the number of days between the dates and then either delete a record or display (for test purposes only) the difference.  Why do I see a blank screen them when this code is run? Shouldn't I see the day difference? :confused:

<?php
// Connect to database.
// Access the database 

@mysql_select_db($database,$con) or die( "Unable to select database");

$result = mysql_query("SELECT * FROM table");

function dateDiff ($d1, $d2) {
// Return the number of days between the two dates:

  return round(abs(strtotime($d1)-strtotime($d2))/86400);

}  // end function dateDiff

while($row = mysql_fetch_array($result))
{
  $days = dateDiff(date("Y-m-d"),$row['DateCreated']);
  if ($days >= 30) 
  {
$update_rec = mysql_query("DELETE FROM table WHERE AutoNum = {$row['AutoNum']}");
if (!$update_rec) {
      die('Invalid query: ' . mysql_error());
    }
  } else {
    echo $days;
    echo "<br />";
  }
}
mysql_close($con)
?>

Link to comment
https://forums.phpfreaks.com/topic/239179-waiting-for-output-between-two-dates/
Share on other sites

Hello,

 

Are you sure your query is returning anything?  If it isn't you are not going to fall into that while loop you have.

 

Also, it could be possible that column name "DateCreated" is incorrect.  Looking at your dateDiff function, if your second date ($d2) does not exist, then it will return the amount of days since the beginning of unix time. For example, say $d1 is today and $d2 does not exist you will end up with 15000+ days.

 

<?php
$d1 = date("Y-m-d");
echo round(abs(strtotime($d1)-strtotime($d2))/86400);

 

In that case, you will always fall into the first part of your if statement which does not echo anything out.  You need to be careful with that because you may delete rows you do not intend to.  You may want to add some more checking to your function to make sure those two variable exist.

 

To check some stuff out, you should modify you while loop to see whats being returned.  Try changing that while loop temporarily to be this:

 

<?php
echo "Rows being returned: " . mysql_num_rows($result);
while($row = mysql_fetch_array($result)){
echo "<pre>";
print_r($row);
echo "Date Difference: " . dateDiff(date("Y-m-d"),$row['DateCreated']);
echo "</pre>";
}

 

If this doesn't give you any insight into what may be causing the issue, then I would need to see what your database table structure is.

 

Let me know.

2-d

where in that code are you connecting to the DB?

 

also:

- take rid of that "@" ... your are suppressing possibles error there... not good.. best if you control the errors.

- include the 2 lines in my signature immediately after your <?php line to control the errors display during development.

- Don't do the DELETE in the while loop... just store the affected ID's in the loop and make just one DELETE by the end of the loop... more efficient.

-And in top of everything... why are you doing all of that if you can do the same just with the DELETE clause?

http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_datediff

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.