从mysql中的timestamp字段过滤日期

时间:2015-12-12 04:40:03

标签: php mysql

我已经使用自定义时区在表格中保存了行。现在我想从这些表中检索数据,只需今天

所以这就是我试过的

date_default_timezone_set('America/Los_Angeles'); 
$dt = new DateTime();
$today = $dt->format('Y-m-d'); //outputs 2015-12-07
$ok = mysqli_query($sqli,"SELECT * FROM `table` WHERE `date` = '$today'" );

我的行包含时间戳格式的日期,如2015-12-07 22:42:02

但我得到空洞的结果。

3 个答案:

答案 0 :(得分:1)

试试这个:

$ok = mysqli_query($sqli,"SELECT * FROM `table` WHERE DATE(date) = CURDATE()" );

根据时区转换时间:ConvertTimeZone

答案 1 :(得分:0)

您应该在将日期传递给mysql之前将日期转换为时间戳:

date_default_timezone_set('America/Los_Angeles'); 
$dt = new DateTime();
$today = $dt->format('Y-m-d'); //outputs 2015-12-07

$ok = outputs("SELECT * FROM `table` WHERE DATE_FORMAT(date,'%Y-%m-%d')= $today" );

答案 2 :(得分:0)

如果$ today =' 2015-12-07 22:42:02&#39 ;;您的查询将给出结果。

$today='2015-12-07 22:42:02';
$ok = mysqli_query($sqli,"SELECT * FROM `table` WHERE `date` = '$today'" );

否则传递今天的日期和下一个日期并检索给定的值

$today='2015-12-07';
$next='2015-12-08';
$ok = mysqli_query($sqli,"SELECT * FROM `table` WHERE `date` >= '$today' and `date` <= '$next'  " );

有关详细信息,请参阅此Oracle SQL : timestamps in where clause How to compare Timestamp in where clause