Get this month's date: code as follows:
1 functionGetMonth ($date){2 $firstday=Date("y-m-01",Strtotime($date));3 $lastday=Date("Y-m-d",Strtotime("$firstday+1 month-1 Day "));4 return Array($firstday,$lastday);5}
$firstday is the first day of the month, if the year is 2014-2, then the $firstday will be 2014-02-01, then according to the $firstday plus one months is 2014-03-01, another day is 2014-02-28, It's so convenient to use date () and Strtotime ().
Get last month's date: code as follows:
1 functionGetlastmonthdays ($date){2 $timestamp=Strtotime($date);3 $firstday=Date(' y-m-01 ',Strtotime(Date(' Y ',$timestamp).‘ -‘. (Date(' m ',$timestamp)-1). ' -01 '));4 $lastday=Date(' y-m-d ',Strtotime("$firstday+1 month-1 Day "));5 return Array($firstday,$lastday);6}
Last month's date needs to get a timestamp, and then in the month-1 OK, super smart Date () will 2014-0-1 this thing converted into 2013-12-01, too cool.
Get next month's date:
The code is as follows:
1 functionGetnextmonthdays ($date){2 $timestamp=Strtotime($date);3 $arr=getdate($timestamp);4 if($arr[' mon '] = = 12){5 $year=$arr[' Year '] +1;6 $month=$arr[' mon ']-11;7 $firstday=$year.‘ -0 '.$month.‘ -01 ';8 $lastday=Date(' y-m-d ',Strtotime("$firstday+1 month-1 Day "));9}Else{Ten $firstday=Date(' y-m-01 ',Strtotime(Date(' Y ',$timestamp).‘ -‘. (Date(' m ',$timestamp) +1). ' -01 ')); One $lastday=Date(' y-m-d ',Strtotime("$firstday+1 month-1 Day ")); A } - return Array($firstday,$lastday); -}
The code for the next month's date looks a bit longer because date () doesn't go like this 2014-13-01, it will go straight back to 1970, so we need to deal with the problem of December, except for December the direct month +1 is OK.
PHP Gets the date of last month, next month, month