Reputation: 27
I'm currently building a website where user can post(like a tweet on twitter), but I want to limit the number of post a user can submit on the website every hour.
This is what I have coded so far and it outputs the total number of post a user have.
$counter = mysql_query("SELECT COUNT(*) AS post_userID FROM post");
$num = mysql_fetch_array($counter);
$count = $num["post_userID"];
echo("$count");
if($count > 2) {
echo("You have exceeded the posting limits, please try again in 24 hours");
}
MY POST TABLE
+------------+------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+------------+------+-----+---------+----------------+
| postID |int(11) | NO | PRI | NULL | auto_increment |
| post_userID|int(11) | NO | | NULL | |
| message |VARCHAR(140)| NO | | NULL | |
| time |datetime | NO | | NULL | |
+------------+------------+------+-----+---------+----------------+
As you can see, I have a time(datetime) stored on my database and it holds the time when the post was submitted.
EXAMPLE:
+------------+------------+------------+----------+
| postID |post_userID | message | time |
+------------+------------+------------+----------+
| 1 | 25 | Hello Mike |1413620228|
| 2 | 26 | Hi John! |1413620332|
+------------+------------+------------+----------+
Oh btw, my server type is MySQL.
Upvotes: 0
Views: 262
Reputation: 1103
I would assume that you have a users
table. Create a column in the users table called last_post_submit_time
and then whenever a user submits the post save that time in it. Finally whenever a user submits a post check if the current time - it's last_post_submit_time >= one hour
. I hope you get it...
Upvotes: 1
Reputation: 7991
One way to do this would be:
select count(*) from post
where post_userID = 25
and time >= date_add(now(), INTERVAL -1 HOUR);
This will give you the number of posts for the user in the past hour.
Upvotes: 1
Reputation: 3129
You can simply divide the timestamp by 3600 and round it to integer, something like this:
SELECT
ROUND(time/3600) AS hours_since_epoch
FROM
posts
WHERE
port_userID = <user> AND hours_since_epoch = ROUND(UNIX_TIMESTAMP(NOW())/3600)
This will return you the list posts user posted this hour (roughly).
Another way is to see how much the user has posted for the last hour:
SELECT
COUNT(*)
FROM
posts
WHERE
port_userID = <user> AND time > UNIX_TIMESTAMP(NOW()) - 3600
Upvotes: 0