zero_ZX Posted April 30, 2012 Share Posted April 30, 2012 Hi, So I only want my users to be able to perform certain tasks each 12 hours. This is the code I use: function canVote($ip, $vote_id, $updateTimer = true){ $time = date("Y-m-d H:i:s"); $this->CI->db->where('vote_id', $vote_id); $this->CI->db->where('user_ip', $ip); $this->CI->db->from('votes_voters'); $count = $this->CI->db->count_all_results(); /*$count = $this->CI->db ->where('vote_id =', $vote_id) ->where('user_ip =', $ip) ->from('votes_voters') -count_all_results();*/ $row = $this->CI->db ->where('vote_id =', $vote_id) ->where('user_ip =', $ip) ->get('votes_voters') ->row(); if($count == 0){ $data = array( 'vote_id' => $vote_id, 'user_ip' => $ip, 'last_vote' => $time ); $this->CI->db->insert('votes_voters', $data); return true; } else{ $last_vote = $row->last_vote; if($last_vote + strtotime("12 hours") < $time){ return false; } else{ if($updateTimer = true){ $data = array( 'last_vote' => $time, ); $this->CI->db->where('vote_id', $vote_id); $this->CI->db->where('user_ip', $ip); $this->CI->db->update('votes_voters', $data); } return true; } } } Apparently the failing bit is this: if($last_vote + strtotime("12 hours") < $time){ return false; } I believe you can guess what I'm trying to do here, if variable 1 + 12 hours is smaller than variable 2, then return false. Any help is much appreciated. Link to comment https://forums.phpfreaks.com/topic/261858-php-dates/ Share on other sites More sharing options...
Barand Posted April 30, 2012 Share Posted April 30, 2012 try if($last_vote + 43200 < $time){ return false; } 86400 seconds = 1 day Link to comment https://forums.phpfreaks.com/topic/261858-php-dates/#findComment-1341770 Share on other sites More sharing options...
zero_ZX Posted April 30, 2012 Author Share Posted April 30, 2012 This didn't work either. In mysql (where i get last_vote) it says: 2012-04-30 20:25:37 so a datetime. Hope it doesn't matter? Link to comment https://forums.phpfreaks.com/topic/261858-php-dates/#findComment-1341789 Share on other sites More sharing options...
Barand Posted April 30, 2012 Share Posted April 30, 2012 Ensure $last_vote and $time are in time format (strtotime() ) first Link to comment https://forums.phpfreaks.com/topic/261858-php-dates/#findComment-1341801 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.