PDO-返回日期列等于本月的所有行,但日期的格式为unix时间戳


PDO - Return all rows where date column equals this month, however date is formatted as unix timestamp

我正在尝试用PHP连接到MySQL的PDO。

$thismonth = date('Ym');
print ($thismonth);

=2201502

我想获取时间戳列(包含unix时间戳)等于上面字符串的所有行(经过某种动态转换),或者可能以另一种方式返回当前月份。

try {
    $sql = $conn->prepare('SELECT id, domain, date FROM `domains` WHERE `date` = :thismonth ');
    $sql->bindParam(':thismonth', $thismonth);
    $sql->execute();
    $results = $sql->fetchAll(PDO::FETCH_ASSOC);
    $conn = null;
    } catch (PDOException $e) {
        print "'n" . "Error!: " . $e->getMessage() . "'n";
        die();
    }
    var_dump($results);

我知道以上是行不通的。这可能是非常错误的。thismonth参数可以是动态比较吗?如有任何援助指向正确的方向,我们将不胜感激。

您需要查找MySQL日期/时间函数的文档并为其添加书签,因为它非常有用:

.... where MONTH(FROM_UNIXTIME(`date`)) = MONTH(NOW())

您尝试过"WHERE date LIKE :thismonth . '%'"吗?