Welcome Guest, Not a member yet? Register   Sign In
Checking if time is more/less then x
#1

[eluser]NyiPHP[/eluser]
I am working with a page counter, and it counts each time the same ip visits an article.

Code:
function enArtikkelTeller($artikkel_id) {
        $ip = $this->input->ip_address();
        $query = $this->db->query("SELECT ip,time,artikkel_id,antall_sett FROM xxx WHERE ip='$ip' AND artikkel_id='$artikkel_id';");
        if(!$query->result()){
            $sql = "INSERT INTO xxx (artikkel_id, ip) VALUES (".$this->db->escape($artikkel_id).", ".$this->db->escape($ip).")";
            $this->db->query($sql);
        }else{
            foreach ($query->result() as $row)
            {
               $teller = $row->antall_sett; //
               $tid = $row->time; // call attributes
                if(strtotime($tid) > strtotime('- 1 minute')) { //1 minutt | * 60 = 1 time * 24 = 1dag/24timer
                    $nyteller = 1+$teller;
                    $data = array(
                        'antall_sett' => $nyteller,
                        //'time' => $tid
                    );
                    
                    $this->db->where('ip', $ip);
                    $this->db->where('artikkel_id', $artikkel_id);
                    $this->db->update('xxx', $data);                  
                }else{
                    
                }              
            }            
        }
    }

the problem is at
Code:
if(strtotime($tid) > strtotime('- 1 minute')) {
                    $nyteller = 1+$teller;
                    $data = array(
                        'antall_sett' => $nyteller,
                        //'time' => $tid
                    );
                    
                    $this->db->where('ip', $ip);
                    $this->db->where('artikkel_id', $artikkel_id);
                    $this->db->update('xxx', $data);                  
                }
i cant make it do the math, check the mysql timestamp $tid and to see if its less then 1 minute.
the time variable is saved on mysql with type: timestamp, on update CURRENT_TIMESTAMP, Standard: CURRENT_TIMESTAMP, Extra: ON UPDATE CURRENT_TIMESTAMP

Im lost in how to solve this out.
1. i need to check if time is less/more
and
2. i need to update the old time with new (current) time, if its less then x-minutes.
#2

[eluser]Jan_1[/eluser]
I like working with unix time more,
instead of MySQL-Timestamp-Format.
May be it's a solution to you. But you would have to set your timestamp with INSERT/Update in your model.

http://www.php.net/manual/en/function.time.php
#3

[eluser]TheFuzzy0ne[/eluser]
I think you might be over-complicating things a little. I see no reason why strtotime() should need to be called more than once.

Code:
$sql_datetime = '2012-04-02 09:55:00';
$ts = strtotime($sql_datetime);

// Has more than a minute passed?
echo ($ts + 60 < time()) ? 'A minute has passed' : 'A minute has not passed';

$ts = time() - 20; // Now minus 20 seconds.

// Has more than a minute passed?
echo ($ts + 60 < time()) ? 'A minute has passed' : 'A minute has not passed';
#4

[eluser]NyiPHP[/eluser]
Thank you very much!

it works now
Code:
foreach ($query->result() as $row)
            {
               $teller = $row->antall_sett;
               $tid = strtotime($row->time); //1000              

               if($tid + 60 < time()) { //1 minutt | * 60 = 1 time * 24 = 1dag/24timer
                   $nyteller = 1+$teller;
                   $data = array(
                       'antall_sett' => $nyteller,
                       //'time' => $ny
                   );
                   $this->db->where('ip', $ip);
                   $this->db->where('artikkel_id', $artikkel_id);
                   $this->db->update('xxx', $data);                  
               }else{
                    
               }              
            }




Theme © iAndrew 2016 - Forum software by © MyBB