title: Result of lottery
body: <p>I am developing a lottery website but stuck with one problem. So please help me out. Here i am explaining the situation:Here is the link of lottery page <a href="http://studysesh.org/greenstreak/mark/lottery1.php" rel="nofollow">http://studysesh.org/greenstreak/mark/lottery1.php</a>. The lottery page having  3 lotteries with different result of time. For example first lottery result out after every six hours, 2nd will result out after 24 hours and 3rd one will result out after 7 days. I have coded all functionality in php. When site is open in any browser,its check for result of Lottery in every second with Ajax request. But, my problem is that if i close the browser than it will not check any value. I have used the cron job of one minute for following code:</p>
  
  <pre><code>    $select = mysql_query("Select * from tbl_rooms where room_status='1'"); 
  $countRows = mysql_num_rows($select);
  
  if($countRows&gt;0)
  {
     while($rows = mysql_fetch_assoc($select))
      {
  
  $date1 = date("Y-m-d H:i:s"); 
  
  $date2 = $rows['room_end_date'];
  
  $date1Timestamp = strtotime($date1);
  $date2Timestamp = strtotime($date2);
  
  if($date1 &lt; $date2){
     //convert the date back to underscore format if needed when printing it out.
    // echo '1 is small='.$date1.','.date('d_m_y',$date1);
  
  //Calculate the difference.
  
  
    $difference = $date2Timestamp - $date1Timestamp;
    //echo $difference;
      if($difference &lt; 60)
      {
  
          sleep($difference);
  
      $room_title=$rows['room_title'];
      $room_id=$rows['room_id'];
  
      if($room_title == 'Quick Six')
      {
          $update_date= date('Y-m-d H:i:s', strtotime('6 hour'));
          mysql_query("UPDATE `tbl_rooms` SET `room_end_date`='$update_date' WHERE `room_title`='Quick Six'"); 
      }   
      if($room_title == 'Daily Draw')
      {
          $update_date= date('Y-m-d H:i:s', strtotime('24 hour'));
          mysql_query("UPDATE `tbl_rooms` SET `room_end_date`='$update_date' WHERE `room_title`='Daily Draw'"); 
      }
      if($room_title == 'Lucky Seven')
      {
          $update_date= date('Y-m-d H:i:s', strtotime('7 day'));
          mysql_query("UPDATE `tbl_rooms` SET `room_end_date`='$update_date' WHERE `room_title`='Lucky Seven'"); 
      }
  
          $buyroom_query=mysql_query("SELECT distinct `u_id` FROM `tbl_buyroom`  where `room_id`='$room_id'");
          $buyroom_count=mysql_num_rows($buyroom_query);
  
          if($buyroom_count &gt;= 5)
          {
              $room_coin=round($rows['room_price']*0.65);
              $winner_query=mysql_query("SELECT distinct `u_id` FROM `tbl_buyroom`  where `room_id`='$room_id' ORDER BY RAND() LIMIT 5 ;");
  
              $i=0;
              while($winner_id=mysql_fetch_assoc($winner_query))
              {
                  $i++;
                  $user_id=$winner_id['u_id'];
                  $a[]=$winner_id['u_id'];
                  $user_query=mysql_query("SELECT * FROM `tbl_users` where `id`='$user_id'");
                  $result_query=mysql_fetch_assoc($user_query);
                  $price=$result_query['total_coin'];
                  if($i==1)
                  {
                      $win_id1=$user_id;
                      $user_coin=0;
                      $user_coin=round($room_coin*0.50);
                      $win_coin=$price+$user_coin;
                      mysql_query("UPDATE `tbl_users` SET `total_coin`='$win_coin' WHERE id='$user_id'");
                      mysql_query("INSERT INTO `tbl_rooms_win`(`room_id`, `u_id`, `win_date`, `price`, `win_status`) VALUES ('$room_id','$user_id','$date1','$user_coin','1')");
                      success_mail($user_id, $room_title, $user_coin, $win_coin);
                      $win[]=$win_coin;
                  }
                  if($i==2)
                  {
                      $win_id2=$user_id;
                      $user_coin=0;
                      $user_coin=round($room_coin*0.25);
                      $win_coin=$price+$user_coin;
                      mysql_query("UPDATE `tbl_users` SET `total_coin`='$win_coin' WHERE id='$user_id'");
                      mysql_query("INSERT INTO `tbl_rooms_win`(`room_id`, `u_id`, `win_date`, `price`, `win_status`) VALUES ('$room_id','$user_id','$date1','$user_coin','1')");
                      success_mail($user_id, $room_title, $user_coin, $win_coin);
                      $win[]=$win_coin;
                  }
                  if($i==3)
                  {
                      $win_id3=$user_id;
                      $user_coin=0;
                      $user_coin=round($room_coin*0.13);
                      $win_coin=$price+$user_coin;
                      mysql_query("UPDATE `tbl_users` SET `total_coin`='$win_coin' WHERE id='$user_id'");
                      mysql_query("INSERT INTO `tbl_rooms_win`(`room_id`, `u_id`, `win_date`, `price`, `win_status`) VALUES ('$room_id','$user_id','$date1','$user_coin','1')");
                      success_mail($user_id, $room_title, $user_coin, $win_coin);
                      $win[]=$win_coin;
                  }
                  if($i==4)
                  {
                      $win_id4=$user_id;
                      $user_coin=0;
                      $user_coin=round($room_coin*0.06);
                      $win_coin=$price+$user_coin;
                      mysql_query("UPDATE `tbl_users` SET `total_coin`='$win_coin' WHERE id='$user_id'");
                      mysql_query("INSERT INTO `tbl_rooms_win`(`room_id`, `u_id`, `win_date`, `price`, `win_status`) VALUES ('$room_id','$user_id','$date1','$user_coin','1')");
                      success_mail($user_id, $room_title, $user_coin, $win_coin);
                      $win[]=$win_coin;
                  }
                  if($i==5)
                  {
                      $win_id5=$user_id;
                      $user_coin=0;
                      $user_coin=round($room_coin*0.06);
                      $win_coin=$price+$user_coin;
                      mysql_query("UPDATE `tbl_users` SET `total_coin`='$win_coin' WHERE id='$user_id'");
                      mysql_query("INSERT INTO `tbl_rooms_win`(`room_id`, `u_id`, `win_date`, `price`, `win_status`) VALUES ('$room_id','$user_id','$date1','$user_coin','1')");
                      success_mail($user_id, $room_title, $user_coin, $win_coin);
                      $win[]=$win_coin;
                  }
  
  
              }
              mysql_query("DELETE FROM `tbl_buyroom` WHERE `room_id`='$room_id'");
  
          }
          else
          {
              while($no_winner=mysql_fetch_assoc($buyroom_query))
              {
                  $uid=$no_winner['u_id'];
                  nowinner_mail($uid, $room_title);
                  $result="No winner";
              }   
          }
      }   
  }
  
  }
  }
  </code></pre>
  
  <p>In the above  code, i have used sleep method.This works perfectly for sometime but many times, if it will sleep for 1 lottery result in the mean time another result will be coming with less seconds of sleep. So it stops working for one lottery. I hope you understand my problem. Please help me out to resolve it</p>