Lytheum Posted March 25, 2007 Share Posted March 25, 2007 In my comments system, I'm trying to restrict people to only post comments within 10 minutes from each other. So basicly what I did was make a table in a database, and when someone posts, it adds the current time to a field called "firstdate" and a time 10 minutes from now to a field called "lastdate". Now the problem is, I do not know how to compare those dates. Most likely because I used a DateAdd() function that I found on the net, and the return value is in this format: $temptime = time(); $firstdate = strftime('%Hh%M %A %d %b',$temptime); // Would return 013h25 Sunday 25 Mar $temptime = DateAdd('n',10,$temptime); $lastdate = strftime('%Hh%M %A %d %b',$temptime); // Would return 13h35 Sunday 25 Mar Does anybody understand what I'm asking for? Maybe a solution beyond all of this, an easier way if it's possible. Quote Link to comment https://forums.phpfreaks.com/topic/44239-solved-comparing-dates/ Share on other sites More sharing options...
JakeD Posted March 25, 2007 Share Posted March 25, 2007 if you could get it in minutes maybe <? $lastpost = (pulls it from the database); $lpost = $lastpost *10; if ($posttime >= $lpost) { echo "you can post now"; } ?> i dk.....maybe something along the lines... Quote Link to comment https://forums.phpfreaks.com/topic/44239-solved-comparing-dates/#findComment-214851 Share on other sites More sharing options...
Barand Posted March 25, 2007 Share Posted March 25, 2007 In the comments table, store NOW() in a DATETIME field when the comment is posted. When they post a comment, <?php $sql = "SELECT COUNT(*) FROM comments WHERE userid = '$id' AND posttime > NOW() - INTERVAL 10 MINUTE"; $res = mysql_query($sql); if (mysql_result($res, 0) > 0) { // there was a post during last 10 mins } else { // ok to post } ?> Quote Link to comment https://forums.phpfreaks.com/topic/44239-solved-comparing-dates/#findComment-214927 Share on other sites More sharing options...
Lytheum Posted March 25, 2007 Author Share Posted March 25, 2007 Awesome Barannd, just what I was looking for. I knew there would be a straight-forward way to do it. Thanks for the help, appreciate it. Thanks JakeD for the help. It seemed you were on the right path, but Barand posted a better way. Quote Link to comment https://forums.phpfreaks.com/topic/44239-solved-comparing-dates/#findComment-214957 Share on other sites More sharing options...
Lytheum Posted March 25, 2007 Author Share Posted March 25, 2007 Awesome Barannd, just what I was looking for. I knew there would be a straight-forward way to do it. Thanks for the help, appreciate it. Thanks JakeD for the help. It seemed you were on the right path, but Barand posted a better way. As an edit to this, I'd like it to display something such as: You can not post another comment for (5) minutes, where (5) is a variable telling them how long they have until they can post again. So something like a now() - posttime ? The thing is, how can I do it with php rather than mysql, because I don't plan on entering it into the DB. And php's time() function is a bit different than the now(). Will it work if I use time() and make it exactly like the Datetime field (0000-00-00 00:00:00) and then do arithmetic operations on it? Quote Link to comment https://forums.phpfreaks.com/topic/44239-solved-comparing-dates/#findComment-214966 Share on other sites More sharing options...
Barand Posted March 25, 2007 Share Posted March 25, 2007 If you want to do it in php, get the time of the user's last post <?php /** * get users last post */ $sql = "SELECT posttime FROM comments WHERE userid = '$id' ORDER BY posttime DESC LIMIT 1"; $res = mysql_query($sql); if (mysql_num_rows($res) > 0) { // there was a post $time_last_post = strtotime(mysql_result($res,0)); } else { $time_last_post = 0; } $time_limit = 5; // mins if ((time() - $time_last_post) > $time_limit * 60) { // ok to post } else { echo "You must wait $time_limit minutes between posts"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/44239-solved-comparing-dates/#findComment-214974 Share on other sites More sharing options...
Lytheum Posted March 25, 2007 Author Share Posted March 25, 2007 Thanks again dude, you've been great! Quote Link to comment https://forums.phpfreaks.com/topic/44239-solved-comparing-dates/#findComment-215022 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.