Alright so whatever i do i cant get this to work.
Now...im not a wizzard or anything but why does it have to be so damn hard to find out if its more than 2 minutes or more in difference by the time in database to now.

The "timed" in database is a `datetime` and looks like this "2011-06-16 14:55:33"


anyone got any solutions?

require_once "db_connect.inc.php";
$thisplace = "delinquency";
$sql = "SELECT timed FROM g_timer WHERE place='$thisplace' AND user='$username'  LIMIT 1";
	$result = mysql_query($sql);
if($result) {
$row = mysql_fetch_assoc($result);
$timed = $row['timed'];
$date = $timed;
if(empty($date)) {
        return "No date provided";
    }
    
    $periods         = array("second", "minute", "hour", "day", "week", "month", "year", "decade");
    $lengths         = array("60","60","24","7","4.35","12","10");
    
    $now             = time();
    $unix_date         = strtotime($date);
    
       // check validity of date
    if(empty($unix_date)) {    
        return "Bad date";
    }

    // is it future date or past date
    if($now > $unix_date) {    
        $difference     = $now - $unix_date;
        $tense         = "ago";
        
    } else {
        $difference     = $unix_date - $now;
        $tense         = "from now";
    }
    
    for($j = 0; $difference >= $lengths[$j] && $j < count($lengths)-1; $j++) {
        $difference /= $lengths[$j];
    }
    
    $difference = round($difference);
    
    if($difference != 1) {
        $periods[$j].= "s";
    }
    
    return "$difference $periods[$j] {$tense}";



//$timer = nicetime($date); // 2 days ago
//echo $timer;
if($periods[$j] =="minutes" or "hours" or "days" or "weeks" or"months" or "years"  and $tense == "ago"){

///Let the user do what he wants
}else echo "You have to wait";

}else echo "No data found in the database";

Dani AI

Generated

Two reliable options solve "is the DB time more than two minutes old": do the check in SQL (fast and atomic) or do it in PHP (simple and precise). 's idea to move the check into the query is excellent for filtering or gating at the DB level; 's UNIX-timestamp approach is the same principle but expressed differently. For a precise two-minute rule, prefer comparing seconds (120) rather than relying on integer-minute rounding.

A compact, robust PHP check avoids the human-readable nicetime routine for logic and works well when one row is being evaluated:

$when = strtotime($row['timed']);
if ($when === false) {
  // handle invalid/empty date
}
if (time() - $when > 120) {
  // allowed: more than 2 minutes have passed
} else {
  // still within 2 minutes
}

Common pitfalls seen in the thread: the boolean chain if ($a == 'x' or 'y' ...) is incorrect because non-empty strings evaluate true; that makes the condition unreliable. Use in_array($value, ['minutes','hours']) or compare each value explicitly. Also watch for query failures or empty fetches (check the query result and DB error) if the script produces no output.

Practical notes: prefer prepared statements (PDO or mysqli) instead of deprecated mysql_* functions, and ensure consistent timezones (store UTC or use TIMESTAMP types). For large sets or indexed checks, a DB-side predicate like "timed < NOW() - INTERVAL 2 MINUTE" is index-friendly and efficient; use a seconds-based DB check if exact 120-second precision is required.

Recommended Answers

All 6 Replies

The output is blank from the code i got up now.

Why not just work it out when querying the database?

The following will check whether there is more than two minutes difference between the Date/Time value, `timed`, and the current time. If it is, the field `lapsed` will be set to 1. Otherwise it'll be 0.

$sql = "SELECT *, IF(TIMESTAMPDIFF(MINUTE, `timed`, NOW()) > 2, 1, 0) AS `lapsed`  
    FROM g_timer 
    WHERE place='{$thisplace}' 
    AND user='{$username}'
    LIMIT 1";

Saves writing spaghetti code in PHP.

R.

Member Avatar for Member #120589

I find it easier to store timestamps (integers) so you can do straightforward arithmetic on them. 2 mins = 120.

so, I think this will work (not tested)

"SELECT field1 FROM table1 WHERE UNIX_TIMESTAMP() > `timed` + 120"

i would have pat the president if i had such knowledge of php/mysql..
cheeers guys.

Why not just work it out when querying the database?

The following will check whether there is more than two minutes difference between the Date/Time value, `timed`, and the current time. If it is, the field `lapsed` will be set to 1. Otherwise it'll be 0.

$sql = "SELECT *, IF(TIMESTAMPDIFF(MINUTE, `timed`, NOW()) > 2, 1, 0) AS `lapsed`  
    FROM g_timer 
    WHERE place='{$thisplace}' 
    AND user='{$username}'
    LIMIT 1";

Saves writing spaghetti code in PHP.

R.

This is what i got now.

$thisplace = "delinquency";
$sql = "SELECT *, IF(TIMESTAMPDIFF(MINUTE, `timed`, NOW()) > 2, 1, 0) AS `lapsed`  
    FROM g_timer 
    WHERE place='{$thisplace}' 
    AND user='{$username}'
    LIMIT 1";
	$result = mysql_query($sql);
	$row = mysql_fetch_assoc($result);
	if($row['lapsed'] == "0"){die("You have to wait, last run was <b>".$row['timed']."</b>");}

But the script dont die after i give it the current time in the mysql.

Got it working, thanks again!

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.