HDTuto.com

PHP - How to get last day of the current, next and previous month?


I remember, when I was working on my core php project and it was something on timetable related project and I require to get last day of current month, get last day of next month and also require to get last month of the previous month. It was easy to find a way to get last day of because of php. so I did that and we also get last day of the specific month too by add date.

We can do it using date() and strtotime() of PHP. So i added all examples, you can see and use it that you need or requirement for your project.

Get Last Day Of The Current Month:

$lastDay = date('t',strtotime('today'));

print_r($lastDay);

Get Last Day Of The Current Month 2:

$lastDateOfThisMonth =strtotime('last day of this month') ;

$lastDay = date('d/m/Y', $lastDateOfThisMonth);

print_r($lastDay);

Get Last Day Of The Next Month:

$lastDay = date('t',strtotime('next month'));

print_r($lastDay);

Get Last Day Of The Next Month 2:

$lastDateOfNextMonth =strtotime('last day of next month') ;

$lastDay = date('d/m/Y', $lastDateOfNextMonth);

print_r($lastDay);

Get Last Day Of The Previous Month:

$lastDay = date('t',strtotime('last month'));

print_r($lastDay);

Get Last Day Of The Previous Month 2:

$lastDateOfLastMonth =strtotime('last day of last month') ;

$lastDay = date('d/m/Y', $lastDateOfLastMonth);

print_r($lastDay);

Get Last Day Of The Specific Month:

$lastDay = date('t',strtotime('1/1/2018'));

print_r($lastDay);

I hope you found your best...




July 4, 2018 | Category : PHP
Related Posts