我已經在這個網站上搜索了我需要的東西,但我找不到任何我想要的東西。從 mysql 中提取兩個日期之間的數據有點困難。我知道系統如何編寫代碼來過濾兩個日期之間的數據,但我遇到的問題是這兩個日期在不同的月份。讓我進一步解釋一下;我有工作代碼來拉兩個日期之間的日期 - 目前我必須手動設置它們。我需要的是根據當前日期計算兩個日期($from& $till)。$from應始終為一個月的 25 號 (00:00) 和下個月$till的 24 號 (23:59),而當前日期應始終介于兩者之間。示例:2020 年 1 月 7 日$from應為 2019 年 12 月 25 日 00:00$till應為 2020 年 1 月 24 日 (23:59) 2020 年 1 月 25 日$from應為 2020 年 1 月 25 日 00:00$till應為 2020 年 2 月 24 日 (23: 59)目前我有以下代碼,我手動設置日期:<?php $from = strtotime('25/12/2019 00:00'); $till = strtotime('24/01/2020 23:59'); $stmt = $con -> prepare("SELECT SUM(`amount`) as `total` FROM `income` WHERE user_id = ? && time >= ? && time <= ?"); $stmt -> bind_param('iii', $user_id, $from, $till); $stmt -> execute(); $stmt -> store_result(); $stmt -> bind_result($total_income); $stmt -> fetch();?>那么有沒有辦法根據當前時間自動設置這些日期呢?
1 回答

米琪卡哇伊
TA貢獻1998條經驗 獲得超6個贊
要自動設置日期(根據當前日期),您可以執行以下操作:
//default: from 25th of last month to 24th of current month
$from = strtotime('-1 month',strtotime(date('m/25/Y 00:00:00')));
$till = strtotime(date('m/24/Y 23:59:59'));
if (intval(date('d')) > 24) { //current day is bigger than 25 - shift it one month
$from = strtotime('+1 month',$from);
$till = strtotime('+1 month',$till);
}
這將使用上個月的 25 日作為默認開始日期,并將當月 24 日作為默認結束日期。
如果當前日期已經是該月的 25 日(或更晚),則它將兩個日期都移動 1 個月。
另外:如果您在日期中使用斜杠,請記住格式是月/日/年,而不是日/月/年
- 1 回答
- 0 關注
- 148 瀏覽
添加回答
舉報
0/150
提交
取消