PostgreSQL将datetime与时间戳进行比较

时间:2018-01-03 13:04:10

标签: php postgresql

我的postgreSQL表中有datetime字段,我想选择字段,与时间戳变量进行比较,例如:(date_time >= ".$from_date." AND date_time <= ".$to_date)其中$from_date$to_date包含时间戳。我尝试了date("Y-m-d h:m:i",$from_date),但我在<12>或接近“12”时遇到语法错误。执行的SQL语句是:SELECT COUNT(*)FROM“cars_pay_history”“t”WHERE date_time&gt; = 2017-12-03 12:12:00

2 个答案:

答案 0 :(得分:2)

您需要引用并标记您的嵌入日期:

0xAD

答案 1 :(得分:0)

$query  = "SELECT COUNT(*) FROM cars_pay_history AS t ";
$query .= "WHERE date_time >= '".date('Y-m-d H:i:s',$from_date)."' ";
$query .= "AND date_time < '".date('Y-m-d H:i:s',$to_date)."'";

请注意date函数中指定的格式:

h:m:i =
    h => 12-hour format of an hour with leading zeros (it should be 24-hour format instead, unless you use A -AM/PM- to distinguish)
    m => numeric representation of a month, with leading zeros (it should be minutes instead)
    i => minutes with leading zeros (it should be seconds instead)

您应该使用H:i:s代替。