[英]How to use PHP's " .date('Y-m-d'). " in a MySQL query in Where clause?
[英]PHP - MySQL query - counting results where timestamp is within specific date (“Y-m-d”)
這是非常基本的MySQL,但我無法弄清楚這一點,以及如何正確執行。
示例:我有一個名為“ table1”的數據庫表,其中包含用戶訪問者數據的記錄列表。 欄:“ ID”,“ TM”和“ IP”
“ TM”包含記錄存儲時間的時間戳。
我有一個PHP代碼,在其中循環從開始日期到當前日期的幾天。 像這個例子:
// Start date
$startdateforarray = '2010-07-21';
// End date
$end_date = date("Y-m-d");
while (strtotime($startdateforarray) <= strtotime($end_date)) {
$timestamp = strtotime($startdateforarray);
//Here I want to run my MySQL Query...
$startdateforarray = date ("Y-m-d", strtotime("+1 day", strtotime($startdateforarray)));
}
現在,在循環中,我想進行查詢以計算每天“ table1”中有多少個結果。
因此,MySQL查詢應類似於:
"SELECT * FROM table1 WHERE TM = (day of $timestamp)"
當然($ timestamp的一天)是我遇到的問題。 我知道這應該很簡單,但是我還沒有找到解決方案。
假設時間戳是指Unix時間戳,則可以
SELECT * FROM table1 WHERE FROM_UNIXTIME(TM,'%Y-%m-%d') = '2010-07-21'
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.