1

I have the following function but have a problem in that my timestamp field in the database is a mysql timestamp type which is in format '2012-08-30 11:31:41' and the strtotime('-12 hours'); is giving a unix timestamp.

whats the solution?

public function checkIfItemVisited($user_id, $item_id) {

    $timeago = strtotime('-12 hours');
    $params = array(
        ':user_id' => $user_id,
        ':item_id' => $item_id,
        ':timestamp' => $timeago
    );
    $sql = "SELECT `visit_id` FROM `item_visits` WHERE `user_id` = :user_id AND `item_id` = :item_id AND `timestamp` > :timestamp";
    $stmt = $this->query($sql, $params);

    if($stmt->rowCount() > 0) :
        return false;
    else :
        $this->countVisit($user_id, $item_id);
    endif;
}
3
  • 1
    do the comparison in the query Commented Sep 1, 2012 at 6:59
  • I don't understand what you're trying to do here, nor what your problem is. Commented Sep 1, 2012 at 7:00
  • you can save timestamp by an int in mysql Commented Sep 1, 2012 at 7:02

3 Answers 3

2

To solve your timestamp comparison problem, you can use UNIX_TIMESTAMP() in mysql

$sql = "SELECT `visit_id`
        FROM `item_visits`
        WHERE `user_id` = :user_id
               AND `item_id` =  :item_id
               AND UNIX_TIMESTAMP(timestamp) > :timestamp";
Sign up to request clarification or add additional context in comments.

1 Comment

@VinceLowe I cant imagine it is very efficient converting the timestamp every time rather than using the built in functions -well it's your call.
2
 ...   AND `timestamp` > NOW() - INTERVAL 12 HOUR ...

Comments

0

Try this:

SELECT FROM_UNIXTIME(1196440219);

If you want vise-wersa conversion, use this:

SELECT UNIX_TIMESTAMP('2007-11-30 10:30:19');

Source: http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_from-unixtime

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.