我需要检索结果行(从昨天下午3时到今天下午3时),每一行都有DateTime列(时间戳)。
我怎样才能质疑这个问题?
我更喜欢代码点火器活动记录
发布于 2017-03-24 09:00:45
试着做这样的事情:
$this->db->where('`DateTime` BETWEEN "'. strtotime("yesterday 15:00"). '" and "'. strtotime("today 15:00").'"');使用标准时间()函数将文本日期转换为UNIX时间戳,然后使用它进行比较。像strtotime("yesterday 3pm")和strtotime("today 3pm")这样的东西也应该能工作。请注意,与服务器和PHP配置上的locale和安装设置相比,意外的结果可能取决于数据库中不同的时间设置和区域设置。
如果时间戳不是Unix时间戳,则可以尝试以下两种方法:
$this->db->where("`DateTime` BETWEEN '". date('Y-m-d H:i:s', strtotime("yesterday 3pm")) ."'::timestamp AND '". date('Y-m-d H:i:s', strtotime("today 3pm"))."'::timestamp");或:
$this->db->where("`DateTime` BETWEEN '". date('Y-m-d H:i:s', strtotime("yesterday 3pm")) ."' AND '". date('Y-m-d H:i:s', strtotime("today 3pm"))."'");由于这个答案中对@Oto和@pozs的讨论,更简单和更好的方法是使用本地postgresql语法来比较时间戳:
$this->db->where("`DateTime` BETWEEN (current_date - 1) + time '15:00:00' AND current_date + time '15:00:00'");编辑:请注意,如果您的列名为datetime,则需要设置回标以避免与PHP:s datetime-datetime混淆。更新了示例。
UPDATE:如果不使用Unix时间戳,则添加替代语法。第三个示例使用本机postgresql语法来比较时间戳。
发布于 2017-03-24 09:18:14
我不知道代码点火器活动记录,对于“纯”postgres,您可以使用这个:
SELECT * FROM yourtable
WHERE datetime BETWEEN
to_timestamp ((CURRENT_DATE - interval '1 day')::date||' 15:00:00', 'YYYY-MM-DD HH24:MI:SS')
AND
to_timestamp (CURRENT_DATE ||' 15:00:00', 'YYYY-MM-DD HH24:MI:SS')https://stackoverflow.com/questions/42994843
复制相似问题