Compare time before proceeding with a query

Greetings!

I need some help with preventing users from updating certain stats too often as it’s
putting too much strain on the server. Ideally, I would like the below code to first
check my ‘updated’ timestamp against current time, and if it’s less than 30 minutes to display
a message, otherwise proceed with the update.

Currently it looks like this:

[php] if (isset($_GET[‘refresh’]) && $_GET[‘refresh’] == “true”)
{
do stuff…
}[/php]

My last ‘updated’ timestamp can be fetched like this:

[php]$data[‘updated’][/php]

Any ideas?

Thank you.

use strtotime() to get the 30 minutes
[php]$min = strtotime($data[‘updated’], ‘+30 minutes’);
if($data[‘updated’] <= $min) {
echo “You must wait 30 minutes before updating these stats”;
} else {
// proceed with update
}[/php]That’s assuming that $data[‘updated’] is a date or datetime datatype.

Thank you!

I’m currently looking into this, but am also getting a notice along with it:

Notice: A non well formed numeric value encountered in,

which I assume has to do with the way the timestamp is stored (unix time format) in the db.

I’ll see if I can fix it and make it work, and will report back.

Thanks again or your help.

Hi, I can’t wrap my head around this for some reason. How would I do this when the ‘updated’ data
is stored as unix timestamp? I keep getting the ‘A non well formed numeric value encountered in…’,
and can’t find a solution to this.

what is the data type for the update time?

It’s an integer, a bigint(20) in mysql, if that’s what you’re asking. It stores a unix timestamp, so when something updates it inserts a:

[php]$data[‘updated’] = time();[/php] into the database.

When I need to display the last updated time, I have a small function ‘humanTiming’ which I fetch like this:

[php]echo humanTiming ($data[‘updated’]);[/php]

and it prints something like ‘updated 2 hours ago’.

never heard of storing a time stamp like that, but you’ll need to use strtotime on that too.

Sponsor our Newsletter | Privacy Policy | Terms of Service