PHP obtains the date of the previous month, next month, and this month. Because the work needs to get the date of the previous month, next month, and this month, we found the implementation code from the website and shared it to help our friends write programs today, I suddenly found that I needed to get the date of last month, next month, and this month for my work. I found the implementation code from my website.
When I was writing a program today, I suddenly found a function that was written a long time ago to get the number of days of the month. The classic switch version, but when I got the number of days of the previous month, I only set the month to 1, it is estimated that it was too difficult at the time, and then I saw a sense of treasure. I wanted to deal with it again, but I was sure there were some very convenient methods. so I found the following version, A small modification was made. Get Month date: Code: function getMonth ($ date) {$ firstday = date ("Y-m-01", strtotime ($ date )); $ lastday = date ("Y-m-d", strtotime ("$ firstday + 1 month-1 day"); return array ($ firstday, $ lastday );} $ firstday is the first day of the month. if $ date is like 2014-2, $ firstday will be. Then, add a month based on $ firstday to, and then subtract one day from, it is really convenient to use date () and strtotime. Get Last Month date: Code: function getlastMonthDays ($ date) {$ timestamp = strtotime ($ date); $ firstday = date ('Y-m-01 ', strtotime (date ('Y', $ timestamp ). '-'. (date ('M', $ timestamp)-1 ). '-01'); $ lastday = date ('Y-m-D', strtotime ("$ firstday + 1 month-1 day ")); return array ($ firstday, $ lastday);} you need to get a timestamp for the last month's date, and then-1 in the month is OK. the intelligent date () it would be nice to convert 2014-0-1. Get the next month date: the code is as follows: function getNextMonthDays ($ date) {$ timestamp = strtotime ($ date); $ arr = getdate ($ timestamp ); if ($ arr ['mon'] = 12) {$ year = $ arr ['Year'] + 1; $ month = $ arr ['mon']-11; $ firstday = $ year. '-0 '. $ month. '-01'; $ lastday = date ('Y-m-D', strtotime ("$ firstday + 1 month-1 day "));} else {$ firstday = date ('Y-m-01 ', strtotime (date ('Y', $ timestamp ). '-'. (date ('M', $ timestamp) + 1 ). '-01'); $ lastday = date ('Y-m-D', strtotim E ("$ firstday + 1 month-1 day");} return array ($ firstday, $ lastday);} the code for the next month's date looks longer, because date () if it cannot be changed to something like 2014-13-01, it will directly return to 1970. Therefore, we need to handle the issue of December before, except that the month of December is the month + 1. In general, it is very convenient. the date function is too powerful.
Today, when I wrote a program, I suddenly found out that...