0

Possible Duplicate:
In PHP, is there an easy way to get the first and last date of a month?

How can we find First and last day of a date?

I have a date "01/05/2011". i am getting this date dynamically. so i like to know the last day of this date. like '31 or 30 or 28 or 29'.

i want to find it using php...

Community
  • 1
  • 1
Abhilash
  • 104
  • 1
  • 5
  • 2
    Use the search function before asking: http://stackoverflow.com/search?q=first+and+last+day+of+month+php – Gordon Apr 29 '11 at 07:26
  • 1
    and just in case the date format gives you any trouble, see http://stackoverflow.com/questions/5399075/php-datetimecreatefromformat-in-5-2 and http://stackoverflow.com/questions/3348396/converting-a-time-string-to-unix-timestamp – Gordon Apr 29 '11 at 07:33

2 Answers2

4

This function may help you :

function lastday($month = '', $year = '')
    {
       if (empty($month)) {
          $month = date('m');
       }
       if (empty($year)) {
          $year = date('Y');
       }
       $result = strtotime("{$year}-{$month}-01");
       $result = strtotime('-1 second', strtotime('+1 month', $result));
       return date('Y-m-d', $result);
    }

Call like

$month = 3;
$year = 2011;
$lastDay = $this->lastday($month, $year);

That will give you the last day of the March 2011......

Pushpendra
  • 4,344
  • 5
  • 36
  • 64
2
echo date('t', strtotime('01/05/2011'));

should do the trick

Flask
  • 4,966
  • 1
  • 20
  • 39