php last day of last month
PHP: Get last day of month.
This is a short tutorial on how to get the last day / date of a given month using PHP. In the code snippets below, I will show you how to get the last date of this month, as well as the last day of a month from a specified date.
The key to getting the last date of a month is the letter “t”. When used with PHP’s date function, the letter “t” acts as a format character that returns the number of days that are in a given month.
This month.
The last day of this month:
In the code above, we simply provide PHP’s date function with the format characters “Y-m-t”. If you test it out for yourself, you’ll see that that a YYYY-MM-DD date string is returned and that it looks something like this: “2016-08-31”
From a given date.
The last day of a month – from a given date:
In the example above, we have the date string “2014-02-04”, which is a YYYY-MM-DD date for the 4th of February, 2014. To get the last date of February, 2014, we simply convert the date to a UNIX timestamp using PHP’s strtotime function; before using the resulting timestamp as the second parameter in our date function. If you run the PHP code above, you’ll see that the result is: “2014-02-28”.
If you only want to return the number of days in a given month, then simply omit the “Y” and “m” format characters.
Using the DateTime object.
If you’re looking for an OO interface, then you can also make use of the DateTime object:
Note: If you want to get the last date of the current month, then simply create the DateTime object without passing in a string as the constructor parameter.
PHP: Adding months to a date, while not exceeding the last day of the month
Looking to create a function that will do this in PHP.
I need to add a number of months to a date, but not exceed the last day of the month in doing so.
If I use date addition in PHP, I get overruns:
My specification doesn’t allow me to overrun into a new month.
What’s the easiest method to accomplish this?
7 Answers 7
You can compare the day of the month before and after you add 1 month. If it’s not the same, you exceeded the next month.
this seems to work for me and gives yor desired result:
EDIT:
after reading Crend Kings comemnt, i think we need more information here. whats the desired result in the following cases:
in words: should the method add the number of days of the next month, wich is what Crend King does and what gives results like 2011-02-26 and 2011-03-03 (wich doesn’t seem like the desired results to me) or should this add one month and leave the day as is, instead of a day thats «too high» like my code does? i’m confused.
As far as I can tell, this problem is very limited in scope, so you’re likely to be best off by testing for one type of error, and fixing it.
All you want to do is make sure that adding «one month» to a late date like the 29th, 30th or 31st does not push you forward to the 1st, 2nd or 3rd of the next-next month.
The way date_modify() works (using it on an example date «2012-01-31» with a string like «+1 months»), is that it first increases the month number by 1, then finds the 31st day from the start of that month. This is why it spills over to March 3rd.
When this is not what you desired, all you have to do is use date_modify() again, now telling it to go back a few days (3 days in this example). Since you only want to go back to the last day of the previous month, the number of days you will want to go back is always the same as the day-of-month in your faulty date.
All that remains is to make sure you don’t apply this correction when it is not needed, like when PHP were to improves in future. This is relatively easy, because the scope of the possible problem situations is very limited.
My code below adds «+1 month», checks if that has caused the day-of-month to change wildly from something high to something low, and adjusts the date if that’s the case.
Results in: 2012-02-29 (yes, this was a leap year).
PS: If you want to add years, the problem and the symptoms are nearly identical. Again, you just need to to check if the day-of-month resulting is 1/2/3 and the day-of-month going in is 29/30/31. If so, you need to go back «-X days» using date_modify, where X is the resulting day-of-month.