In MySQL: DATE_ADD()
or DATE_SUB()
, then compare.
In PHP: DateTime->diff()
.
سؤال
I want to limit my users to only (be able to) post something every 15 minutes.
So in my SQL query I select NOW()
to get the current date and time, and also the user's last post date date_added
. I want to compare the two dates and if the difference between now()
and date_added
is less than 15 minutes, then the user cannot yet post. If it's greater then he can. If less than 15 minutes then I'd like a message like 'Please wait x minutes and y seconds.' So I need some kind of date manipulation/comparison.
How should I approach this. In MySQL or PHP?
المحلول 2
In MySQL: DATE_ADD()
or DATE_SUB()
, then compare.
In PHP: DateTime->diff()
.
نصائح أخرى
You could simply convert the mysql timestamp into a php-date and compare from there $time = date ("Y-m-d H:i:s", $mysqltime);
You will find lots of useful snippets how to compare dates on the functions documentation: http://php.net/manual/de/function.date.php
Edit: pozs anwser nails it ...
Something like
SELECT count(*) FROM posts WHERE last_post > DATE_SUB(NOW(), INTERVAL 15 minute);