Jump to content

PHP dates


zero_ZX

Recommended Posts

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.