douge3492 2012-09-01 06:57
浏览 65
已采纳

php检查mysql时间戳

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条回答 默认 最新

  • dongshanya2008 2012-09-01 07:00
    关注

    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";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥50 cocos2d-x lua 在mac上接入lua protobuf?
  • ¥15 es 7.17.20版本生成时间戳
  • ¥15 wpf dategrid表头排序图标自定义
  • ¥15 xshell无法连接提示ssh服务器拒绝密码
  • ¥15 AT89C52单片机C语言关于串口通信的位操作
  • ¥20 需要步骤截图(标签-服务器|关键词-map)
  • ¥50 gki vendor hook
  • ¥15 灰狼算法和蚁群算法如何结合
  • ¥15 写一个利用ESP32自带按键和LED控制的录像代码
  • ¥20 stm32f103,hal库 hal_usart_receive函数接收不到数据。