Home > Article > Backend Development > PHP gets the dates of last month, next month, and this month (strtotime(), date())_PHP tutorial
When I was writing a program today, I suddenly discovered a function that I wrote a long time ago to get the number of days in the month. It is a classic switch version. But when I got the number of days in the previous month, I just changed the month to -1. I guess it was too much at that time. I'm sleepy, and I felt a bit creepy when I saw it again. I originally wanted to deal with it again, but then I thought there must be some super convenient method, so I found the version below and made a few minor modifications.
Get the date of this month:
<span 1</span> <span function</span> getMonth(<span $date</span><span ){ </span><span 2</span> <span $firstday</span> = <span date</span>("Y-m-01",<span strtotime</span>(<span $date</span><span )); </span><span 3</span> <span $lastday</span> = <span date</span>("Y-m-d",<span strtotime</span>("<span $firstday</span> +1 month -1 day"<span )); </span><span 4</span> <span return</span> <span array</span>(<span $firstday</span>,<span $lastday</span><span ); </span><span 5</span> }
$firstday is the first day of the month. If $date is 2014-2, $firstday will be 2014-02-01. Then add one month to $firstday to get 2014-03-01, and then subtract one day. It is 2014-02-28. It is so convenient to use date() and strtotime().
Get the date of last month:
<span 1</span> <span function</span> getlastMonthDays(<span $date</span><span ){ </span><span 2</span> <span $timestamp</span>=<span strtotime</span>(<span $date</span><span ); </span><span 3</span> <span $firstday</span>=<span date</span>('Y-m-01',<span strtotime</span>(<span date</span>('Y',<span $timestamp</span>).'-'.(<span date</span>('m',<span $timestamp</span>)-1).'-01'<span )); </span><span 4</span> <span $lastday</span>=<span date</span>('Y-m-d',<span strtotime</span>("<span $firstday</span> +1 month -1 day"<span )); </span><span 5</span> <span return</span> <span array</span>(<span $firstday</span>,<span $lastday</span><span ); </span><span 6</span> }
You need to get a timestamp for the last month’s date, and then add -1 to the month. The super smart date() will convert things like 2014-0-1 into 2013-12-01. It’s so cool. .
Get next month’s date:
<span 1</span> <span function</span> getNextMonthDays(<span $date</span><span ){ </span><span 2</span> <span $timestamp</span>=<span strtotime</span>(<span $date</span><span ); </span><span 3</span> <span $arr</span>=<span getdate</span>(<span $timestamp</span><span ); </span><span 4</span> <span if</span>(<span $arr</span>['mon'] == 12<span ){ </span><span 5</span> <span $year</span>=<span $arr</span>['year'] +1<span ; </span><span 6</span> <span $month</span>=<span $arr</span>['mon'] -11<span ; </span><span 7</span> <span $firstday</span>=<span $year</span>.'-0'.<span $month</span>.'-01'<span ; </span><span 8</span> <span $lastday</span>=<span date</span>('Y-m-d',<span strtotime</span>("<span $firstday</span> +1 month -1 day"<span )); </span><span 9</span> }<span else</span><span { </span><span 10</span> <span $firstday</span>=<span date</span>('Y-m-01',<span strtotime</span>(<span date</span>('Y',<span $timestamp</span>).'-'.(<span date</span>('m',<span $timestamp</span>)+1).'-01'<span )); </span><span 11</span> <span $lastday</span>=<span date</span>('Y-m-d',<span strtotime</span>("<span $firstday</span> +1 month -1 day"<span )); </span><span 12</span> <span } </span><span 13</span> <span return</span> <span array</span>(<span $firstday</span>,<span $lastday</span><span ); </span><span 14</span> }
The code for next month’s date seems a little longer, because date() cannot convert things like 2014-13-01, it will go back directly to 1970, so we need to deal with the issue of December in the front, except for December Just +1 for the month and that’s it.
Generally speaking, it is very convenient. The date function is too powerful.