PHP中得到指定的時間段

2021-09-08 20:52:32 字數 521 閱讀 4897

最近在專案中,遇到個比較特殊的要求,比如,mysql中,比如今天是2023年12月13日,星期1,

如果要取得上週一0:00到週日0:00(就是2023年12月6日0:00到

2023年12月12日週日0:00)之間的記錄,要如何寫呢?

其實可以這樣寫,經過我的探索和發現後知道:

比如你日期欄位是col1

where week(col1,1)=week(from_days(to_days(curdate())-7))

,其中week(***,1)是返回當天所在的星期,是1年中的第幾個星期,引數1表示星期從星期1開始,0

表示為從星期天開始。

而from_days(to_days(curdate())-7,則是返回上週的日期(7天前的),

再用week(from_days(to_days(curdate())-7)),就可以求的上週1到週日,之間所有符合條件的

日期了。

當然,以前可以再進行擴充套件,返回

任意時間段之間的日期拉

查詢指定時間段的資料

一 select from searchdetails where searchdatetime between 2014 10 06 01 40 50.123 and 2014 12 07 23 12 and datepart hour,searchdatetime between 1 and 1...

Sql 按照指定天數時間段查詢

begin try declare begintime datetime 2017 02 01 declare endtime datetime 2017 03 3 23 59 59 declare showtype int 0 展示的型別 0 日 1 十日 2 月 declare cuscount...

查詢符合指定時間段的記錄

查詢要求 查詢日期在每日07 30到10 00之間的所有日期的資料。create table exam opdt datetime,sm varchar 20 go with ex opdt,sm as select 2019 02 01 07 29 59 不符 union all select 2...