我只是想從兩個日期之間的資料庫中獲取資料。這些日期是通過用戶輸入給出的。
<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 < ?";
有人可以幫我設定這些日期的格式,因為我完全迷失了,我嘗試切換格式但沒有成功?
提前致謝。
uj5u.com熱心網友回復:
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_timestamp,dep,resp FROM TABLE_data WHERE created_timestamp > ? 和 created_timestamp < ?";
$stmt=$db->prepare($sql);$stmt->bind_param('ss',$startDate ,$endDate);
$stmt->execute();
$result= $stmt->get_result()->fetch_assoc();
$stmt->close();
}
uj5u.com熱心網友回復:
我認為更好地使用這種方式
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();
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/385856.html
