我使用unix timestamp整數爲我的數據庫中的每個預訂存儲時間。我遇到問題,我的查詢沒有正確選擇,並且似乎是休息一天。WHERE DATE_FORMAT = DATE_FORMAT不準確
例如,在我的網站上,我有一個日曆表,顯示了每個月的每一天。我使用下面的代碼,以填充產品的每一天:現在
$sql = "select b.*, p.name as p_name, p.color as p_color
from `bookings` as b
left join `products` as p on (p.id = b.id_product)
where date_format(from_unixtime(b.timestamp), '%M %e %Y') = date_format(from_unixtime(:timestamp), '%M %e %Y')
order by b.timestamp asc";
$stm = $this->app->db->prepare($sql);
$stm->bindParam(':timestamp', $this->timestamp);
$stm->execute();
$res = $stm->fetchAll();
,它顯示我的1月24日第25天在我的日曆進行的預訂。
的問題似乎在於與查詢的這個部分:
where date_format(from_unixtime(b.timestamp), '%M %e %Y') = date_format(from_unixtime(:timestamp), '%M %e %Y')
例如,當$this->timestamp = '1453698000'
(25日)和b.timestamp = '1453618800'
(24日),它顯示在我的日曆在25日這一紀錄。
我完全不知道爲什麼會發生這種情況。我試着改變date_format查詢來使用'%d-%m-%Y',我試過添加'00:00:00',我嘗試過在PDO構造中手動設置時區,我已經通過使用PHP date()函數回顯時間戳,確保我所有的時區正確排列(它們是)。
這是怎麼發生的?任何幫助將不勝感激。
添加時間喜歡10:00:00從不喜歡00:00:00 – Grumpy
如果您從服務器時間獲得'$ this-> timestanp'您是否檢查了服務器的時區並進行了適當修改 – RiggsFolly