首先,您不应该在 where 子句中的运算符左侧使用 mysql 函数。这样 mysql 需要读取磁盘上的完整表来计算要比较的值,而不是针对速度和资源使用(IO、cpu)优化查询。
根据您的问题和评论,我了解您正在数据库中查询与您的$giorno_selected
代表中的字符串具有相同日期的行。因此,您需要在特定日期找到时间戳在 0:00 到 23:59 之间的所有行:
$giorno_timestamp_start = date_create_from_format('d-m-Y', $giorno_selected)->getTimestamp();
$giorno_timestamp_end = $giorno_timestamp_start + 86399; //add almost all seconds per day onto the start timestamp
$information="SELECT * FROM scheda_anagrafica WHERE time_inserted BETWEEN " . $giorno_timestamp_start. " AND ". $giorno_timestamp_end;
$result1 = mysql_query($information) or die (mysql_error());
while( $row = mysql_fetch_array($result1)) {
echo $row['information1'];
}
如果您的time_inserted
列是类型integer
并且包含 unix_timestampds,则此方法有效。如果它是类型datetime
,或者timestamp
您需要像这样修改查询:
$information="SELECT * FROM scheda_anagrafica WHERE time_inserted BETWEEN FROM_UNIXTIME(" . $giorno_timestamp_start. ") AND FROM_UNIXTIME(". $giorno_timestamp_end .")";