我正在寫一個時間記錄程序的客戶,誰是鋼琴調音師,我已經寫了下面的PHP代碼給創紀錄的「做」狀態:時間計算與MySQL
$last_tuned = '2017-01-05';
$tuning_period = 3;
$month_last_tuned = date('Y-m', strtotime(date('Y-m-d', strtotime($last_tuned))));
$next_tuning = date('Y-m', strtotime($month_last_tuned.(' +'.$tuning_period.' months')));
if (time() > strtotime($next_tuning.' -1 months')) {
if (time() > strtotime($next_tuning)) {
return 'late';
} else {
return 'upcoming';
}
}
如您所見,$last_tuned
變量是日期(YYYY-MM-DD)格式。然後將其轉換爲(YYYY-MM)格式。
一旦被轉換,再增加一個月,與$tuning_period
相同,然後被添加到$month_last_tuned
變量中,給我們一個月和年的值,以便我們需要添加新記錄。
如果當前時間(與time()
一起找到)大於$next_tuning
變量 - 1個月,則返回該任務爲upcoming
。如果它位於$next_tuning
變量之後,則返回該任務爲late
。
我現在必須編寫一個MySQL查詢來列出即將到來或已晚歸還的項目。
我該怎麼寫這個在MySQL?我對MySQL的功能不是很滿意,有些幫助會非常感謝。
我在邏輯嘗試:
SELECT * FROM records
// The next lines are to get the most recent month_last_tuned value and add the tuning_period variable
WHERE
NOW() > (SELECT tuning_date FROM tunings ORDER BY tuning_date ASC LIMIT 1)
+
(SELECT tuning_period FROM records WHERE records.id = INITIAL CUSTOMER ID)
我知道,這是完全錯誤的。雖然邏輯很多。
我的數據庫架構如下:
我期待的行從查詢返回的是在標準桿與在PHP代碼上面的「晚」或「即將到來的」值。這意味着返回的行將在其下一個調整日期的1個月內(根據上次調整加上調整週期計算)。
謝謝!
掛起,那'$ month_last_tuned'行...你是否將'$ last_tuned'轉換爲一個時間,然後轉換爲'date()',然後*返回*時間,所以你可以將它格式化爲'日期'再次沒有「日」部分??? –
更新你的問題添加表模式..一個適當的數據樣本和預期的結果..請 – scaisEdge
@NiettheDarkAbsol - 這是將某種格式的時間戳轉換爲另一種格式?有一個更好的方法嗎? –