Jump to content

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/44239-solved-comparing-dates/
Share on other sites

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
} 
?>

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. :P

 

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?

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";
} 
?>

 

 

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.