【问题标题】:how to search between two mysql unix timestamp?如何在两个mysql unix时间戳之间搜索?
【发布时间】:2017-08-25 06:49:46
【问题描述】:

嗨,mysql unix 时间戳存储在我表的日期列中,现在我正在从日期选择器中搜索两个字段,即from_dateto_date,但我没有找到任何记录尝试了很多方法但没有成功我的日期选择器日期是这样的 08/22/2017 而存储在表中的 unix 时间戳就像

1503380449
1503380428
1503380326

如何触发 mysql 查询以获取两个日期之间的记录我尝试了 mysql UNIX 时间戳功能但不起作用。

我试过这个:-

SELECT * FROM tbl_example WHERE
my_date BETWEEN 
STR_TO_DATE('2017/08/22', '%Y %D %M %h:%i:%s %x')
AND
STR_TO_DATE('2017/08/22', '%Y %D %M %h:%i:%s %x')

我试过这个:-

SELECT * FROM tbl_example WHERE
my_date BETWEEN 
UNIX_TIMESTAMP(STR_TO_DATE('2017/08/22', '%M %d %Y %h:%i%p')
AND
UNIX_TIMESTAMP(STR_TO_DATE('2017/08/22', '%M %d %Y %h:%i%p')

我也试过这个:-

SELECT * FROM tbl_example WHERE
my_date BETWEEN 
FROM_UNIXTIME(UNIX_TIMESTAMP(STR_TO_DATE('Aug 22 2017 12:00AM', '%M %d %Y %h:%i%p')))
AND
FROM_UNIXTIME(UNIX_TIMESTAMP(STR_TO_DATE('Aug 22 2017 12:00AM', '%M %d %Y %h:%i%p')))

谢谢

【问题讨论】:

  • 你必须解析 datepicker 的值来改变它的时间戳来比较,你应该看看php.net/manual/en/function.strtotime.php
  • @MacBooc 我想要使用存储在 mysql 表中的时间戳的特定日期的所有记录
  • 你试过我的回答了吗?
  • @MacBooc 我正在使用 zend 框架我编写了这段代码 $select->where("my_date BETWEEN UNIX_TIMESTAMP('".$from_date."00:00:00') AND UNIX_TIMESTAMP('". $to_date."00:00:00')");但它不工作,而如果我触发它的查询,那么它的工作,但我没有得到相同的日期结果意味着如果从日期和到日期相同,那么我没有得到记录
  • 然后我将编辑我的代码,因为这里的问题是我在一天的开头放了两个日期,告诉我你是否可以

标签: php mysql date unix timestamp


【解决方案1】:

如果你想在 mysql 中做你应该使用 UNIX_TIMESTAMP 函数

PHP:

$from_date = "08/22/2017";
$from_date = substr($from_date, 6, 4) . "-" . substr($from_date, 0, 2) . "-" . substr($from_date, 3, 2);
$to_date = "08/30/2017";
$to_date = substr($to_date, 6, 4) . "-" . substr($to_date, 0, 2) . "-" . substr($to_date, 3, 2);

MYSQL:

"SELECT * FROM tbl_example WHERE
my_date BETWEEN 
UNIX_TIMESTAMP('".$from_date." 00:00:00')
AND
UNIX_TIMESTAMP('".$to_date." 23:59:59')" // changed here to have the end of the day in case that the query is on the same day

或 php:

$from_date = strtotime("08/22/2017");
$to_date = strtotime("08/30/2017");

MYSQL:

"SELECT * FROM tbl_example WHERE
my_date BETWEEN 
".$from_date."
AND
".$to_date

【讨论】:

  • 但我想建议您,与其使用 substr 进行转换,我们可以简单地使用这种方式 date('Y-m-d',strtotime("08/22/2017"));你说什么?
  • 是的,这也是个好主意,但是如果你想使用它,你可以直接使用 strtotime("08/22/2017") 来获得时间戳,我以为你想在SQL 我会编辑给你看
猜你喜欢
  • 1970-01-01
  • 2014-06-27
  • 1970-01-01
  • 2018-01-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多