jbis2k Posted June 13, 2011 Share Posted June 13, 2011 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? <?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) ?> Quote Link to comment https://forums.phpfreaks.com/topic/239179-waiting-for-output-between-two-dates/ Share on other sites More sharing options...
2-d Posted June 14, 2011 Share Posted June 14, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/239179-waiting-for-output-between-two-dates/#findComment-1229428 Share on other sites More sharing options...
mikosiko Posted June 14, 2011 Share Posted June 14, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/239179-waiting-for-output-between-two-dates/#findComment-1229510 Share on other sites More sharing options...
jbis2k Posted June 15, 2011 Author Share Posted June 15, 2011 Thanks for both replies. I will try your suggestions in a few days and report back with the results. Quote Link to comment https://forums.phpfreaks.com/topic/239179-waiting-for-output-between-two-dates/#findComment-1229806 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.