【问题标题】:select posts from the last month [duplicate]选择上个月的帖子[重复]
【发布时间】:2017-03-28 11:55:09
【问题描述】:

posts 表中,我有一个名为date 格式的列 - datetime - current timestamp 并且只需要选择上个月的帖子。

列中的所有日期均来自 2016 年 12 月。例如 - 2016-12-09 04:25:00

<?php
$start = strtotime("-1 month"); // 1488285716
$stmt = $db->query("SELECT * FROM posts where date > " . $start . " order by...");
?> 

但是所有帖子都被选中了!结果应为零。
有什么帮助吗?

【问题讨论】:

    标签: php mysql


    【解决方案1】:

    使用这个

    $start = strtotime("-1 month");
    $date =  date('Y-m-d h:i:s', $start);
    $stmt = $db->query("SELECT * FROM posts where date > " . $date . " order by...");
    

    【讨论】:

    • 您可以从纯 sql 中执行此操作,而无需在 php 中格式化日期时间。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-31
    相关资源
    最近更新 更多