【问题标题】:mysql query select yesterday, today, and tomorrowmysql查询选择昨天、今天和明天
【发布时间】:2012-12-31 19:40:22
【问题描述】:

我正在做一个项目,我必须从数据库中选择所有日期为昨天、今天和明天的条目。 日期作为时间戳存储在数据库中。

到目前为止我有这个 PHP 代码

$hour = 12;
$today = strtotime("$hour:00:00");
$yesterday = strtotime('-1 day', $today);
$tomorrow = strtotime('+2 day', $today);
$today = get_date($today,"timestamp");

$result = $DB->query("SELECT product_id FROM " . DB_PREFIX . "expiry WHERE expiry_date BETWEEN $yesterday AND $tomorrow");

它不工作。请帮忙。

【问题讨论】:

  • 您的日期以什么格式存储在数据库中? strtotime 返回一个 Unix 时间戳。您可能需要使用UNIX_TIMESTAMP(expiry_date)。您是否收到任何错误消息?您确实没有向我们提供足够的信息来回答您的问题。

标签: mysql timestamp


【解决方案1】:

试试这个

 $tomorrow = strtotime('+1 day', $today);

然后 sql 变成了这个

   BETWEEN '".$yesterday."' AND '".$tomorrow."'

并确保

   `" . DB_PREFIX . "expiry` 

不是这样的

  `" . DB_PREFIX . "_expiry`
                    ^---------------did have this or something else like -

【讨论】:

  • 非常感谢。它终于起作用了。哇,自 12 月 25 日以来,我一直在寻找解决方案:(
猜你喜欢
  • 2016-05-28
  • 2014-05-25
  • 2011-05-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-13
相关资源
最近更新 更多