我只是想在两个日期之间从数据库中获取数据。这些日期是通过用户输入给出的。
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
startdate: <input type="date" name="from_date">
enddate: <input type="date" name="to_date">
<input type="submit" name="date" id="date">
</form>
但我没有得到任何数据,因为我认为日期格式是错误的。
if (isset($_POST["date"])) {
echo $startDate = date("Y-m-d", strtotime($_POST['from_date'])); // Y-m-d strtotime
echo $endDate = date("Y-m-d", strtotime($_POST['to_date']));
echo gettype($startDate); // something weird is happening with the dates random dates slip between date ranges
$sql = "SELECT A,C,D,F,G,H,I,K,created_timestamp,changed_timestamp,dep,resp FROM TABLE_data WHERE created_timestamp > ? and created_timestamp < ?";
//$sql = "SELECT A,C,D,F,G,H,I,K,created_timestamp,changed_timestamp,dep,resp FROM TABLE_data WHERE created_timestamp > '2021-01-01' and created_timestamp < '2021-01-31'";
$stmt = $db->prepare($sql);
$stmt->execute([$startDate, $endDate]);
//$stmt->execute();
$result = $stmt->fetchAll();
该查询有效:
$sql = "SELECT A,C,D,F,G,H,I,K,created_timestamp,changed_timestamp,dep,resp FROM TABLE_data WHERE created_timestamp > '2021-01-01' and created_timestamp < '2021-01-31'";
这个不起作用:
$sql = "SELECT A,C,D,F,G,H,I,K,created_timestamp,changed_timestamp,dep,resp FROM TABLE_data WHERE created_timestamp > ? and created_timestamp < ?";
有人能帮我设置这些日期的格式吗?因为我完全迷路了,我试图切换格式,但没有成功?
提前谢谢。
if (isset($_POST["date"])) {
$startDate = date("Y-m-d", strtotime($_POST['from_date']));
$endDate = date("Y-m-d", strtotime($_POST['to_date']));
$sql = "SELECT
A、 C,D,F,G,H,I,K,created_timestamp,changed_timestamps,dep,resp FROM TABLE_data WHERE created_timeStamps>并创建时间戳<quot;;
$stmt=$db->prepare($sql);$stmt->bind_param('ss',$startDate ,$endDate);
$stmt->execute();
$result= $stmt->get_result()->fetch_assoc();
$stmt->close();
}
我认为最好使用这种方式
echo $startDate = date("Y-m-d", strtotime($_POST['from_date']));
echo $endDate = date("Y-m-d", strtotime($_POST['to_date']));
$sql = "SELECT A,C,D,F,G,H,I,K,created_timestamp,changed_timestamp,dep,resp FROM TABLE_data WHERE created_timestamp > '$startDate' and created_timestamp < '$endDate'";
或
$sql = "SELECT A,C,D,F,G,H,I,K,created_timestamp,changed_timestamp,dep,resp FROM TABLE_data WHERE created_timestamp > ? and created_timestamp < ?";
$stmt->bind_param("is", $startDate, $endDate);
$stmt->execute();