假设我的 unix 时间戳为 1497664242
,即 Saturday, June 17, 2017 1:50:42 AM
我想查找表中具有同一天时间戳的所有记录。
我尝试设置一个时间戳,例如1494979200
,即Saturday, June 17, 2017 12:00:00 AM
,我认为我可以使用当天的一般时间并拉取当天匹配的所有记录。
我尝试从特定日期开始循环并查找与该天匹配的所有记录,然后增加 1 天直到今天04/12/2019
$start_date = "05/17/2017";
while ($start_date != "04/12/2019") {
$timestamp = strtotime($start_date);
$sql_select = "SELECT * FROM table WHERE DATE(timestamp) = DATE(FROM_UNIXTIME($timestamp))";
$result_select = $GLOBALS['db']->query($sql_select);
while ($row = $result_select->fetch_array()) {
// will do stuff here
}
$start_date = date("m/d/Y", strtotime($start_date. "+1 day"));
}
最佳答案
如果您将时间戳转换为日期,即 2017-06-17 以及数据库上的时间戳字段,您应该会获得当天的任何行
现在我们知道 timestamp
列实际上被定义为 VARCHAR(),您必须这样做
SELECT *
FROM table
WHERE DATE(FROM_UNIXTIME(timestamp)) = '$start_date';
所以对于你的实际代码
$start_date = "2017-05-17";
while ($start_date != "2019-04-12") {
$sql_select = "SELECT *
FROM table
WHERE DATE(FROM_UNIXTIME(timestamp)) = '$start_date'";
$result_select = $GLOBALS['db']->query($sql_select);
while ($row = $result_select->fetch_array()) {
// will do stuff here
}
$start_date = date("Y-m-d", strtotime($start_date. "+1 day"));
}
关于php - 查找另一个unix时间戳mysql同一天内的所有记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55642156/