Cameron
Cameron

Reputation: 28793

Maths on PHP Timestamps

I have the following block of PHP:

if ($data->timestamp > (time() - 10 * 60) ) {
 // data is newer than 10 mins
} else {
 // data is older than 10 mins
}

That basically says if the timestamp stored in $data is older than 10 minutes.

I'm not sure how this is worked out exactly, and I've read about the time function here: http://php.net/manual/en/function.time.php

I want to edit it to become every hour instead of 10 minutes.

Can anyone share some advice/resources? e.g. how I would change it to become 1 hour and how exactly this is worked out.

Upvotes: 0

Views: 66

Answers (2)

Kevin Lynch
Kevin Lynch

Reputation: 24703

it is worked out starting from the right to left in seconds. One hour would be

if ($data->timestamp > (time() - 60 * 60);

Formatt

time() + (7 * 24 * 60 * 60);
// 7 days; 24 hours; 60 mins; 60secs

You are multiplying it as you work back 60 multiplied by 60 minutes etc etc

Upvotes: 1

A. Kalantari
A. Kalantari

Reputation: 133

This code line checks if the timestamp saved in the $data is NOT older than 10 minutes.

the time() function gives UNIX tiemstamp of the machine in seconds. If you want to change it to an hour its 60 minutes in 60 seconds 60*60. Your code will be like this:

if ($data->timestamp > (time() - 60*60) )

Upvotes: 1

Related Questions