Note that there are some explanatory texts on larger screens.

plurals
  1. POChecking time was within past 24 hours in PHP
    text
    copied!<p>I have developed a referral system where logged in members can send referrals to there family/friends to recommend them to sign up.</p> <p>Everything works fine but yesterday decided I would limit the maximum referrals someone could send within a 24 hour period. I have limited this to 3 referrals maximum per day.</p> <p>I posted the bit of code that I seem to be having problems with below. The problem I am having is that no matter what it seems I get the error message saying I have reach the maximum referrals for today. I'm not sure what I am doing wrong in my code.</p> <pre><code>// referral query $referral_limit = mysql_query("SELECT 'created_on' FROM 'user_referrals' WHERE `referrer_uid` = $referrer_uid ") or die(mysql_error()); if(mysql_num_rows($referral_limit) &gt; 0){ while($row = mysql_fetch_assoc($referral_limit)){ $db_time = $row['created_on']; if((time() - $db_time) &gt; 86400){ // is within 24 hours and has reached maximum daily referral allowance $error[] = "You have reached the maximum referrals for today."; } } } </code></pre> <p>I did try and echo out $db_time and when I do all I get returned is the field name which is <strong>created_on</strong> and not the actual value which in this case should display the timestamp. The <strong>created_on</strong> field in database contains the timestamp a referral was made and I check this to ensure the referring user has not made a referral within the past 24 hours.</p> <p>You will also notice I have not added the extra bit that restricts it to 3 per day but I did not want to add that bit until I can fix this problem first.</p> <p>The database table looks like this:</p> <pre><code>CREATE TABLE IF NOT EXISTS `user_referrals` ( `id` int(11) NOT NULL AUTO_INCREMENT, `referrer_uid` int(11) NOT NULL, `recipient_username` varchar(15) NOT NULL, `referrer_email` varchar(254) DEFAULT NULL, `referred_id` char(32) NOT NULL, `referred_email` varchar(254) NOT NULL, `status` char(9) NOT NULL, `created_on` int(11) NOT NULL, `updated_on` int(11) DEFAULT NULL, PRIMARY KEY (`id`), UNIQUE KEY `referred_id` (`referred_id`), KEY `referrer_uid` (`referrer_uid`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=72 ; </code></pre> <h2>Edit</h2> <p>Here is my code after some assistance. It still says a referral was made in past 24 hours even though there isn't.</p> <p>I think I am doing the error checking wrong. </p> <pre><code>$referral_limit = mysql_query(" SELECT COUNT(*) FROM `user_referrals` WHERE `referrer_uid` = $referrer_uid AND `created_on` &gt; UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 1 DAY))") or die(mysql_error()); if($referral_limit &gt; 0) { $error[] = "You have reached the maximum referrals for today."; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload