Jack
Jack

Reputation: 16724

timestamp check if 10 minutes ago

I have a data in a text file saved using date("Y-m-d h:i:s", strtotime("+2 minutes")) that I need to check if it's 10 minutes ago. I'm trying the following code but it doesn't print anything even if more than 10 minutes ago.

  $now = date("Y-m-d h:i:s", strtotime("now"));
  if($now > strtotime($old_data))
       echo 'expired!';

Upvotes: 5

Views: 18829

Answers (2)

Leandro Garcia
Leandro Garcia

Reputation: 3228

You should change either of the following:

$now = strtotime(date("Y-m-d h:i:s", strtotime("now")));

or

if (strtotime($now) > strtotime($old_data))

I'll go for the second. You are comparing a timestamp over date that is why you are not satisfying the condition of if().

Furthermore, you can also use time() if you're only concern is the current timestamp.

$now = time();

or

$now = date("Y-m-d h:i:s", strtotime("now")); // Remove this line

if(time() > strtotime($old_data)) // Change $now with time()

Upvotes: 2

devBorg
devBorg

Reputation: 224

your comparing a formatted date with a time stamp which explains why nothing works

here:

$now = strtotime("-10 minutes");

if ($now > strtotime($old_data) {
  echo 'expired!';
}

Upvotes: 14

Related Questions