php - 如果 UNIX_TIMESTAMP 在 SQL 查询中更小?

标签 php mysql

如果 unix 时间戳小于数据库中记录的日期时间并且我遇到了一些问题,我将尝试仅从数据库获取数据。

这是我到目前为止得到的;

date_default_timezone_set("UTC");
$now = date("Y-m-d H:i:s", time());
$timestamp = strtotime($now); 
$timestamp2 = strtotime('-3 days', $timestamp);

$sql = "SELECT g.game_id, UNIX_TIMESTAMP(g.lastdraw)+259200 AS dbtimestamp FROM ".$prefix."_games g 
            INNER JOIN ".$prefix."_gameplayer gp
                ON g.game_id = gp.fk_game_id
            WHERE dbtimestamp < $timestamp2 AND gp.fk_player_id=$currplayer AND g.finished=0";
$result = mysql_query($sql);
while($row = mysql_fetch_assoc($result)){
        //if($row['dbtimestamp'] < $timestamp2){
        $thisgameid = $row['game_id'];

        if($thisgameid == 257748){
            echo '<b>GameID: '.$thisgameid.'</b><br>';
        } else {
            echo 'GameID: '.$thisgameid.'<br>';
        }
        echo 'DBtimestamp: '.$row['dbtimestamp'].'<br><br>';
    //}
}

我没有通过这种方式得到任何记录。但是,如果我删除 sql 查询中的“dbtimestamp < $timestamp2”并删除“if($row['dbtimestamp'] < $timestamp2){”的注释,一切正常吗?

我哪里出错了?

提前致谢:-)

最佳答案

当您完全可以使用 native 日期/时间操作直接在 MySQL 中执行此操作时,您为什么要搞乱 PHP 时间戳和 MySQL 日期-> 时间戳值?

SELECT ..
WHERE g.lastdraw < (NOW() - INTERVAL 3 DAY)

关于php - 如果 UNIX_TIMESTAMP 在 SQL 查询中更小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14072046/

相关文章:

php - codeigniter 的哪个 ORM?

php - 如何使用 laravel 5.2 获取 Blade 文件(HTML)中的最后一部分 url?

javascript - 无法将值传递给 php

mysql - 在 vb.net 中自动生成字母数字 ID

php - mysql 服务器不接受 id 和 datetime 作为新记录

java - Hibernate 中的 DateDiff

php - MySQL trigger help, capturing a variable from php insert into 求助

php - 内部数组的 array_unique

mysql - scalikejdbc 中的批量插入在远程计算机上速度很慢

mysql - 无法弄清楚复杂的查询