How to subtract 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 15 minutes before they can view the page. Is this code correct?

+1


a source to share


2 answers


Yes, the code is correct, you can try it just with a smaller time limit.

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

      

Just to be sure.



Try this to see what your variables print 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";
}

?>

      

+2


a source


The time () function returns the number of seconds since Epoch. 15 minutes equals 900 seconds, not 300. Also, the code is correct.



0


a source







All Articles