3raser Posted April 7, 2011 Share Posted April 7, 2011 My site allows users to vote once per hour, and it checks the next time they vote if it's been 3600 seconds (1 hour) since their last vote. But, it always returns the value 0. $grab = mysql_fetch_assoc($has_voted); $calc = strtotime($grab['date']) - strtotime(date("M-d-Y")); if($calc > 3600) { mysql_query("DELETE FROM votes WHERE ip = '". $_SERVER['REMOTE_ADDR'] ."'"); header("location:votes.php?id=". $id .""); } else { echo echo "You've voted within the last hour. Please wait ". $calc ." seconds."; } Quote Link to comment https://forums.phpfreaks.com/topic/232934-strtotime-to-check-if-they-voted-within-the-last-hour/ Share on other sites More sharing options...
Zurev Posted April 7, 2011 Share Posted April 7, 2011 What is $grab['date'] formatted as, does it actually include the time as well, or is it just the format M-d-Y, in which case it would always return 0 unless it were 1 or more days apart. Quote Link to comment https://forums.phpfreaks.com/topic/232934-strtotime-to-check-if-they-voted-within-the-last-hour/#findComment-1198060 Share on other sites More sharing options...
Pikachu2000 Posted April 7, 2011 Share Posted April 7, 2011 Would something like this be easier for you? $query = "DELETE FROM votes WHERE ip = '{$_SERVER['REMOTE_ADDR']}' AND `field_that_holds_datetime` < DATE_SUB( NOW(), INTERVAL 1 HOUR )" mysql_query( $query ); Quote Link to comment https://forums.phpfreaks.com/topic/232934-strtotime-to-check-if-they-voted-within-the-last-hour/#findComment-1198061 Share on other sites More sharing options...
kenrbnsn Posted April 7, 2011 Share Posted April 7, 2011 What is the format of $grab['date']? To get the current date in seconds, all you need is time. Ken Quote Link to comment https://forums.phpfreaks.com/topic/232934-strtotime-to-check-if-they-voted-within-the-last-hour/#findComment-1198062 Share on other sites More sharing options...
3raser Posted April 7, 2011 Author Share Posted April 7, 2011 I tried time(), but it just lets users vote as much as they want. o.O if(mysql_num_rows($has_voted) > 0) { $has_voted = mysql_query("SELECT date FROM votes WHERE ip = '". $_SERVER['REMOTE_ADDR'] ."'"); $grab = mysql_fetch_assoc($has_voted); $calc = $grab['date'] - time(); if($calc > 3600) { mysql_query("DELETE FROM votes WHERE ip = '". $_SERVER['REMOTE_ADDR'] ."'"); header("location:vote.php?id=". $id .""); } else { echo "You've voted within the last hour. Please wait ". $calc ." seconds."; } } else { if($_POST['name_vote_f'] && $_POST['id_vote_f']) { echo "You have successfully voted!"; mysql_query("INSERT INTO votes VALUES (null, '". $_SERVER['REMOTE_ADDR'] ."', '". time() ."')"); mysql_query("UPDATE sites SET votes = votes + 1 WHERE id = '$id'"); } else { $grab = mysql_fetch_assoc($query); echo "<b>Voting for ". $grab['title'] .". Are you sure?</b><br/><br/> <form action='vote.php' method='POST'> <input type='hidden' name='name_vote_f' value='". $grab['title'] ."'> <input type='hidden' name='id_vote_f' value='". $id ."'> <input type='submit' value='Yes, vote now!'> </form>"; } } Quote Link to comment https://forums.phpfreaks.com/topic/232934-strtotime-to-check-if-they-voted-within-the-last-hour/#findComment-1198075 Share on other sites More sharing options...
kenrbnsn Posted April 7, 2011 Share Posted April 7, 2011 Again, what does $grab['date'] contain. Also, you want to subtract the old value ($grab['date']) from the current time. When you do it your way, you will get a negative number which will always be less than 3600. :-) Ken Quote Link to comment https://forums.phpfreaks.com/topic/232934-strtotime-to-check-if-they-voted-within-the-last-hour/#findComment-1198084 Share on other sites More sharing options...
3raser Posted April 7, 2011 Author Share Posted April 7, 2011 Again, what does $grab['date'] contain. Also, you want to subtract the old value ($grab['date']) from the current time. When you do it your way, you will get a negative number which will always be less than 3600. :-) Ken Thanks! And $grab['date'] is just time(). EDIT: Still no difference. Quote Link to comment https://forums.phpfreaks.com/topic/232934-strtotime-to-check-if-they-voted-within-the-last-hour/#findComment-1198089 Share on other sites More sharing options...
kenrbnsn Posted April 7, 2011 Share Posted April 7, 2011 Do some debugging on your own. Put echo statements in to show what values are being used: <?php $has_voted = mysql_query("SELECT date FROM votes WHERE ip = '". $_SERVER['REMOTE_ADDR'] ."'"); $grab = mysql_fetch_assoc($has_voted); $cur = $time(); echo "Current time: $cur<br>\n"; $saved = $grab['date']; echo "Saved time: $saved<br>\n"; $diff = $cur - $saved; echo "Diff: $diff<br>\n"; ?> This should show where the problem lies. Ken Quote Link to comment https://forums.phpfreaks.com/topic/232934-strtotime-to-check-if-they-voted-within-the-last-hour/#findComment-1198191 Share on other sites More sharing options...
3raser Posted April 7, 2011 Author Share Posted April 7, 2011 Do some debugging on your own. Put echo statements in to show what values are being used: <?php $has_voted = mysql_query("SELECT date FROM votes WHERE ip = '". $_SERVER['REMOTE_ADDR'] ."'"); $grab = mysql_fetch_assoc($has_voted); $cur = $time(); echo "Current time: $cur<br>\n"; $saved = $grab['date']; echo "Saved time: $saved<br>\n"; $diff = $cur - $saved; echo "Diff: $diff<br>\n"; ?> This should show where the problem lies. Ken Thanks ken, I figured it out. It seems $has_voted was in the wrong place. Quote Link to comment https://forums.phpfreaks.com/topic/232934-strtotime-to-check-if-they-voted-within-the-last-hour/#findComment-1198443 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.