Creating a simple timestamp and testing if it has expired

php

Solution

current time is obtained with `time()` function, so `$timestamp` should contain the time obtained from the external source, like database, or whatever, to be checked against current time, to see if it has expired:

//test timestamp
$timestamp = 1340037073;

//check if it has expired (600 = 60*10 seconds = 10 minutes)
if ((time() - $timestamp) < 600)
{
  echo 'valid';
}
else
{
  echo 'expired';
}

Hope it works for you :)

Problem

I am trying to learn how to create and test for the expiration of a timestamp. I set up a simple example, trying to test for a timestamp that has expired, but it does not seem to work correctly, and I was wondering if anyone could help me figure out why it wouldn't echo 'valid' and 'expired' in the appropriate times? ``` <?php echo' <html> <head> </head> <body>'; //set static current time $timestamp = 1340037073; if($timestamp <= strtotime("+10 minutes", $timestamp)){ echo "Valid"; }else{ echo "Expired"; } echo' </body> </html>'; ?> ```

Original source