strtotime 獲取上乙個月

2022-09-04 17:12:10 字數 1258 閱讀 4415

1.案例

strtotime結合-1 month, +1 month, next month 有時候會出現錯誤

date("y-m-d",strtotime("-1 month"))

// 輸出:2018-07-01

-1 month 程式設定-30天,相當於7月31日-30天得7月30號

var_dump(date("y-m-d", strtotime("-1 month", strtotime("2017-03-31"))));

//輸出2017-03-03

var_dump(date("y-m-d", strtotime("+1 month", strtotime("2017-08-31"))));

//輸出2017-10-01

var_dump(date("y-m-d", strtotime("next month", strtotime("2017-01-31"))));

//輸出2017-03-03

var_dump(date("y-m-d", strtotime("last month", strtotime("2017-03-31"))));

//輸出2017-03-03

2.解決

php5.3開始, date 新增了一系列修正短語, 來明確這個問題

那就是 first day of 和 last day of,也就是你可以限定好不要讓 date 自動規範化

var_dump(date("y-m-d", strtotime("last day of -1 month", strtotime("2017-03-31"))));

//輸出2017-02-28

var_dump(date("y-m-d", strtotime("first day of +1 month", strtotime("2017-08-31"))));

////輸出2017-09-01

var_dump(date("y-m-d", strtotime("first day of next month", strtotime("2017-01-31"))));

////輸出2017-02-01

var_dump(date("y-m-d", strtotime("last day of last month", strtotime("2017-03-31"))));

////輸出2017-02-28

php獲取當前的上乙個月和下乙個月

由於專案當中有用到這樣的乙個方法,故在這邊做記錄 function getmonth sign 1 else 另外在做時間的需要注意 如果以日期作為結束時,轉換成時間戳會導致時間有差異的 比如我使用日期是20160821,不計算後面的時間,但系統實際會計算乙個時間,即 20160821 00 00 ...

php時間 獲取上乙個月,本月天數,下乙個月

獲取當前月的天數 i 2 y 2013 echo date t strtotime y i 獲取本月日期 如下 function getmonth date firstday是月份的第一天,假如 date是2014 2這樣的話,firstday就會是2014 02 01,然後根據 firstday加...

使用Shell獲取上乙個月 星期的時

bin bash date today date d 1 day ago y m d 最近7天 date befor 7day date d 7 day ago y m d 上乙個完整周 周一到週日 ofday date u stepofday expr ofday 6 date befor wee...