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
https://forums.phpfreaks.com/topic/261858-php-dates/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.