I am try to figure out how to check current time against time in database and header the user to "expired page" if the current time is greater than the one in database "1 hour"

$email = $token = $check_time = "";

$crossToken = $db->prepare("SELECT email, token, check_time FROM pw_reset_company WHERE token=? LIMIT 1");
$crossToken->bind_param('s', $c);
if ($crossToken->execute()) {
    $crossToken->bind_result($email, $token, $check_time);
    $crossToken->store_result();
    $rowCount = $crossToken->num_rows;
    if ($crossToken->fetch()) {
        $contactEmail = $email;
        $timeToExpire = $check_time;
    }
    if ($rowCount > 0) {
        date_default_timezone_set("UTC");
        $checkTime = date("H:i:s", time());
        if($timeToExpire>$checkTime){
            header("Location:?pid=expired");
        }
        ?>
        <form action="<?php $_SERVER['PHP_SELF'] ?>" method="post">
            <label for="password">Enter New Password</label>
            <input type="password" name="password" id="password">
            <label for="confirm">Enter New Password</label>
            <input type="password" name="confirm" id="confirm">
            <input type="submit" name="subNewPass" id="subNewPass" value="Reset Password">
        </form>
    <?php
    } elseif ($_GET['c'] && !(empty($_GET['c']))) {
        header("Location:?pid=expired");
    }
}

Dani AI

Generated

Good call to the direction taken by — the reliable fix is to compare absolute instants (timestamps or full datetimes) instead of time-of-day strings. The original issue usually comes from one of three things: using H:i:s (which drops the date), doing the math in the wrong direction (creating a future check time instead of verifying whether now is past the expiry), or having PHP and MySQL use different timezones. Converting both sides to the same canonical instant avoids those traps.

Two practical, robust approaches that avoid subtle bugs:

  • Prefer a single DB-side check so the database does the expiry math atomically, e.g.
    SELECT 1 FROM pw_reset_company WHERE token = ? AND check_time > UTC_TIMESTAMP() LIMIT 1

    This keeps PHP simpler and removes timezone/format mismatch risks.

  • If you must check in PHP, parse the DB value with a DateTime/DateTimeImmutable in a fixed timezone (UTC), compute the allowed interval, then compare DateTime objects — avoid comparing formatted strings.

Troubleshooting checklist: confirm the column type (DATETIME vs TIMESTAMP) and what timezone the DB is storing; log the raw DB value and PHP's parsed timestamp while debugging; make sure no output is sent before calling header() and immediately follow header() with exit() so execution stops; and validate the token exists before doing any time math. ’s SQL idea of comparing times server-side is on the right track for that reason.

Lastly, harden the workflow: make tokens single-use, store expiry as an explicit field (or store a hashed token), clear/mark tokens after successful reset, and keep everything UTC for storage/display conversion. See the PHP header() docs and DateTimeImmutable docs for precise behavior and the MySQL date/time functions for server-side comparisons.

Recommended Answers

All 4 Replies

I had a same problem but i need a minutes, so you get a new time then subtract 1 hour or in my case 10 minutes from it and then you ask is that time greater then that time in database if it is you do what you want to do if not they your result from SQL will be nothing and that is it...
Code:

$newTime = date("Y-m-d H:i:s");

$timeLess10m = date("Y-m-d H:i:s", strtotime('-10 minutes', strtotime($newTime)));

$SQL="SELECT * FROM `DB` WHERE `ID` = 1 AND `TIME` < '" . $timeLess10m ."'";

$rez = mysql_query($SQL) or die("Error: ".mysql_error());
$num_rows = mysql_num_rows($rez);
if ($num_rows > 0) {
    // 10 minutes is passed
} else {
    // we are still in 10 minutes period...
}

I hope this would help, Mike.

Hi milil I edit my code to this

if ($rowCount > 0) {
        $newTime = date("Y-m-d H:i:s");
        $checkTime = date("Y-m-d H:i:s", strtotime('+1 hour', strtotime($newTime)));
        if ($dbTime < $checkTime) {
            header("Location:?pid=companyManagePasswords&mp=expire");
        }

and I think it's the same what you have provided but still no luck
any other ideas

check_time is a datetime, time, or a varchar (with timestamp) field in the database? I am asking this because in your question you seem to check from a time field but in your response you check for a date-time field. Also, in your response your $checkTime will always be 1 hour after current time. And in your if condition that header() will always be written.

If the field in the database is of datetime type, why not try this:

$currentTime = time();
$expireTime = strtotime($dbTime)+3600; // if $dbTime is datetime field in database
if ($expireTime < $currentTime) {
            header("Location:?pid=companyManagePasswords&mp=expire");
        }

Dear Adrian_5 thanks really you solved my problem it's working now fine

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.