MySQL通过截断小时,分钟和秒来按时间戳过滤

时间:2014-11-10 23:51:05

标签: php mysql sql

我已连接datetimepicker客户端以根据日期范围过滤结果,但我无法选择日期选择器日期以在服务器上包含日期。因此,例如,如果我选择dateTo为2014年11月6日,结果将不包括该记录,但如果我选择2014年11月7日,它将包括2014年11月6日。我确定这必须做使用服务器上的unixtimestamp包括H:M:S,但我无法弄清楚如何截断它们。我尝试使用DATE_FORMAT甚至FROM_UNIXTIME使用'%M%d,%Y',但返回的范围并非正确,这似乎是目前为止最接近的。

PHP将2014年11月6日(或等效日期)转换为时间戳并附加到SQL查询:

if( !empty($query_filters['dateFrom']) && !empty($query_filters['dateTo']) ) 
{
    $sql .= " AND UNIX_TIMESTAMP(packageinstance.created) 
              BETWEEN " . strtotime($query_filters['dateFrom']) . " 
              AND " . strtotime($query_filters['dateTo']);
}
else if( !empty($query_filters['dateFrom']) && empty($query_filters['dateTo']) ) 
{
    $sql .= " AND UNIX_TIMESTAMP(packageinstance.created) >= " . strtotime($query_filters['dateFrom']);
}
else if( !empty($query_filters['dateTo']) && empty($query_filters['dateFrom']) ) 
{
    $sql .= " AND UNIX_TIMESTAMP(packageinstance.created) <= " . strtotime($query_filters['dateTo']);
}

生成SQL

SELECT SQL_CALC_FOUND_ROWS 
     packageinstance.id, 
     UNIX_TIMESTAMP(packageinstance.created) as epoch_time, 
     user.username, 
     status.name as status, 
     package.type 
FROM packageinstance 
JOIN user    ON user.id    = packageinstance.creatorid  
JOIN status  ON status.id  = packageinstance.status 
JOIN package ON package.id = packageinstance.packageid 
WHERE packageinstance.locationid = 1 
AND status LIKE '%%' 
AND UNIX_TIMESTAMP(packageinstance.created) < 1415228400 
ORDER BY epoch_time DESC LIMIT 5 OFFSET 0 

从packageinstance.created中截断此SQL查询的时间需要做什么?

更新

使用下面的@alu答案进行PHP日期格式化,然后添加DATE_FORMAT这似乎正在起作用,但是这个过度杀手是非常冗长的吗?没有单独使用@alu回答,但它有助于发挥作用。

if( !empty($query_filters['dateFrom']) && !empty($query_filters['dateTo']) ) 
{
    $sql .= " AND DATE_FORMAT(packageinstance.created, '%Y-%m-%d') 
              BETWEEN DATE_FORMAT('" . date('Y-m-d 00:00:00', strtotime($query_filters['dateFrom'])) . "', '%Y-%m-%d')  
              AND DATE_FORMAT('" . date('Y-m-d 00:00:00', strtotime($query_filters['dateTo'])) . "', '%Y-%m-%d')";
}
else if( !empty($query_filters['dateFrom']) && empty($query_filters['dateTo']) ) 
{
    $sql .= " AND DATE_FORMAT(packageinstance.created, '%Y-%m-%d') >= 
              DATE_FORMAT('" . date('Y-m-d 00:00:00', strtotime($query_filters['dateFrom'])) . "', '%Y-%m-%d')";
}
else if( !empty($query_filters['dateTo']) && empty($query_filters['dateFrom']) ) 
{
    $sql .= " AND DATE_FORMAT(packageinstance.created, '%Y-%m-%d') <= 
              DATE_FORMAT('" . date('Y-m-d 00:00:00', strtotime($query_filters['dateTo'])) . "', '%Y-%m-%d')";
}

生成SQL

SELECT SQL_CALC_FOUND_ROWS 
     packageinstance.id, 
     UNIX_TIMESTAMP(packageinstance.created) as epoch_time, 
     user.username, 
     status.name as status, 
     package.type 
FROM packageinstance 
JOIN user    ON user.id    = packageinstance.creatorid  
JOIN status  ON status.id  = packageinstance.status 
JOIN package ON package.id = packageinstance.packageid 
WHERE packageinstance.locationid = ? AND status LIKE '%%' 
AND DATE_FORMAT(packageinstance.created, '%Y-%m-%d') <= DATE_FORMAT('2014-11-06 00:00:00', '%Y-%m-%d') 
ORDER BY epoch_time desc LIMIT 5 OFFSET 0 

1 个答案:

答案 0 :(得分:1)

试试这个。

 $sql .= " AND packageinstance.created >= '" . date('Y-m-d 00:00:00', strtotime($query_filters['dateFrom'])) . "'";