Search code examples
phpmysqlunix-timestamp

mysql Unix timestamp check if 2 hours elasped


i am stuck with trying to figure out this issue its been a long time since i have done mysql calls.

i want to check by rows "user_id" , "entry" and "time" and see if at least 30 minutes have passed and if it hasnt alert the user

my code so far is :

$chkquery = "SELECT * FROM wpqi_myCRED_log WHERE entry='quiz' AND user_id='1' AND time < unix_timestamp() - 1800";

and then if it has been more than 30 minutes then maybe something like this

    if( $time_diff >= 1800) {
    echo "Yay! It has been 30 minutes!";

} else {
    $remaining = (1800 - $time_diff );
    echo "Wait! Its not been 30 minutes\n";
    echo "please come back in ".date ( "i:s" , $remaining)." minutes";
}

I dont want them to do the quiz more than every 30 minutes thx in advance


Solution

  • ok this is what i ended up doing, it is probably messy and can be cleaned up but seems to be working any suggestions i'm all ears!

    $user_id ="1";
    $refType="completing_quiz_full";
    
    //Minutes allowed between plays
    $minutesAllowed = 120;
    
    // take minutes allowed and subract 
    $get2hour = time() - ($minutesAllowed * 60);
    
    $chkquery = "SELECT * FROM wpqi_myCRED_log WHERE ref='$refType' AND user_id='$user_id' AND time >= $get2hour order by time desc limit 1";
    $chk = mysql_query($chkquery) or die($chkquery."<br/><br/>".mysql_error());
    $num_rows = mysql_num_rows($chk);
    
    //echo "this many rows meet that criteria is : ". $num_rows. "<br>" ; 
    
    if ($num_rows > 0) {
        while($row = mysql_fetch_array($chk, MYSQL_ASSOC)){
            $entry = $row['entry'];
            $coin = str_replace("%plural%","Burst",$entry);
            echo "<br> You already received ". $coin. "<br>" ;  
    
           // get time from Table
           $then =$row['time'];
           $played = date('h:i:s A', $then);
    
          //Get the current timestamp.
           $now = time();
    
          //Calculate the difference.
          $difference = $now - $then;
    
          //Convert  to nearest minute
          $minutes = floor($difference / 60);
    
          //now subtract minutes we allowed
          $timeLeft = $minutesAllowed - $minutes;
    
          echo "You Have ". $timeLeft. " Minutes till you can play again<br> You Last Played at ". $played. "<BR>";
    
          }
    
          } Else {
    
         echo " now we can show your the content cause its been over 2 hours!! "; 
    
          }