Pregunta

This is what i am trying to do.

 id ip        timestamp           userid
  9 127.0.0.1 2013-06-27 16:52:49 35

I would like to first query table to see if ip for the user is persisted. If so, he/she voted 2) Check timestamp of vote against current timestamp, if his voting time is way after 24 hours, then allow him to vote again. Ensure he can only vote once in 24 hours.

This is my current zend_db_select statement. Has some problems coming out with the logic

function checkIfCanLikeH($hlid,$ip){
    $canlike = false;
    $currenttime = time();
    $checkquery = $this->dbo->select()
       ->from("hl", array("num"=>"COUNT(*)"))
       ->where("hlid = ?", $hlid)
       ->where("ip = ?", $ip)
       ->where("timestamp < ".new Zend_Db_Expr('NOW()'));
    $this->dbo->setFetchMode(Zend_Db::FETCH_ASSOC);
    $checkrequest = $this->dbo->fetchRow($checkquery);
    if($checkrequest['num']!=0){
        $ip = true;
    }
    return $ip;
}

I am stucked at how do i compare the timestamp. I need retrieve the timestamp from table and check if the current time is smaller than value in table (added 24 hours).

Advice appreciated.

¿Fue útil?

Solución

->where("timestamp < DATE_SUB(NOW(), INTERVAL 1 DAY)");

This is the advantage of using MySQL's timestamp/datetime/date column types - you can use its inbuilt date and time functions. And I'm pretty sure you don't need Zend_Db_Expr here.

Edit: I'm not completely clear what you're asking, but I think the WHERE clause above does what you want, just the other way around. You could do this instead:

->where("DATE_ADD(timestamp, INTERVAL 1 DAY) < NOW()");

which is the same thing but written a different way. This means "where timestamp + 1 day is less than the current time".

If it helps, try running this query on your database:

SELECT DATE_ADD(timestamp, INTERVAL 1 DAY), NOW() FROM hl

so you can see how the DATE_ADD() function in MySQL works.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top