如何在两个mysql unix时间戳之间进行搜索?

时间:2017-08-25 06:49:47

标签: php mysql date unix timestamp

你好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')))

感谢

1 个答案:

答案 0 :(得分:0)

如果你想在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