yumii
yumii

Reputation:

How to deduct time in php?

I wrote this

<?php
session_start();

if(!isset($_SESSION['now'])){
$_SESSION['now'] = time();
}

if(time() > ($_SESSION['now'] + 300)){
echo "15 minutes have passed";
} else {
echo "waiting";
}

?>

What I want to do is, let the user wait for 15 minutes before he/she can view the page. Is the code correct?

Upvotes: 1

Views: 952

Answers (2)

Bobwise
Bobwise

Reputation: 702

The time() function returns the number of seconds since the Epoch. 15 minutes is equal to 900 seconds, not 300. Other than that the code is correct.

Upvotes: 0

&#211;lafur Waage
&#211;lafur Waage

Reputation: 69991

Yes the code is correct, you can try it simply by having a smaller time constraint.

if(time() => ($_SESSION['now'] + 900)){

Just to be extra sure.

Try this to see what your variables are printing out.

<?php
session_start();

if(!isset($_SESSION['now'])){
$_SESSION['now'] = time();
}

if(time() > ($_SESSION['now'] + 10)){
    echo date("d.m.Y H:i", time())."<br />";
    echo date("d.m.Y H:i", $_SESSION['now'])."<br />";
    echo "15 minutes have passed";
} else {
    echo date("d.m.Y H:i", time())."<br />";
    echo date("d.m.Y H:i", $_SESSION['now'])."<br />";
    echo "waiting";
}

?>

Upvotes: 2

Related Questions