0

i need to check if a timestamp is expired datetime, by comparing that to the current timestamp

So i have :

$current_timestamp = time();
$db_timestamp = 134500809

My dubt is, is it correct in all cases to do (is it always TRUE):

if($current_timestamp > $db_timestamp){
 // $db_timestamp is expired for sure
}

or should this be FALSE in some cases?

itsme
  • 48,972
  • 96
  • 224
  • 345

1 Answers1

1

It will be false if the timestamp from the database is in the future, thus it will work properly.

Try inserting a row in your database and set the timestamp to time() + 3600, then you'll see that your if statement is correctly evaluated and returning false until one hour has passed.

Karl Laurentius Roos
  • 4,360
  • 1
  • 33
  • 42
  • oh sure i just need to check if current timestamp is major then the db timestamp, doesn't matter if in future, so i can go for using my if statment? :) – itsme Nov 18 '12 at 12:59
  • If you mean that you're checking the database clock it's most likely due to different timezones, if you want them in sync I'd recommend setting both the server and database clock to UTC: http://stackoverflow.com/questions/947299/how-do-i-make-mysqls-now-and-curdate-functions-use-utc – Karl Laurentius Roos Nov 18 '12 at 13:02
  • php and database has same clock timezone sorry, so i can go easly as shown above? :D – itsme Nov 18 '12 at 13:06
  • Yes, you can use it. But if they'd turn out to be exactly the same and you still want to execute the statement you should use the `>=` (larger than or equals) operator instead of `>`, just a tip. Good luck! – Karl Laurentius Roos Nov 18 '12 at 13:09