0

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";
3
Contributors
6
Replies
7
Views
6 Years
Discussion Span
Last Post by Sorcher
0

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.

1

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"
0

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

0

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.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.