php get last day of given month

You can use DateTime methods:

$month = '02';

$date = new DateTime(date('Y').'-'.$month.'-01');
$date->modify('last day of this month');
$last_day_this_month = $date->format('Y-m-d');

there are many ways to do that, i give you two answers\ideas:

1- Try to use strtotime PHP function (http://php.net/manual/es/function.strtotime.php)

Something like date("Y-m-d", strtotime("last day of this month")); or first day... or any month.

2- Other way you can use that:

First day:

date("Y-m-d", mktime(0, 0, 0, *YOUR MONTH PARAM*,1 ,date("Y")));

Last day:

date("Y-m-d", mktime(0, 0, 0, *YOUR MONTH PARAM*+1,0,date("Y")));

Read about mktime function here:

http://php.net/manual/es/function.mktime.php

Good luck!


I have had this problem with PHP before, try the following way:

$dateToTest = "2015-02-01";
$lastday = date('t',strtotime($dateToTest));

Tags:

Datetime

Php

Date