Praveen Kumar
Praveen Kumar

Reputation: 89

Getting The End Date of the Given Month

I need to get the end date of the given month for some calculation purpose,

how can I do that in PHP, I tried using date() function, but It didn't work.

I used this:

date($year.'-'.$month.'-t');

But this gives the current month's end date. I think I'm wrong somewhere, I couldn't find where I'm going wrong here.

If I give year as 2012 & month as 03, then it must show me as 2012-03-31.

Upvotes: 0

Views: 3505

Answers (9)

Faisal
Faisal

Reputation: 4765

If you are using PHP >= 5.2 I strongly suggest you use the new DateTime object. For example like below:

$a_date = "2012-03-23";
$date = new DateTime($a_date);
$date->modify('last day of this month');
echo $date->format('Y-m-d');

Upvotes: 0

Uttara
Uttara

Reputation: 2532

function getEndDate($year, $month)
{
   $day = array(1=>31,2=>28,3=>31,4=>30,5=>31,6=>30,7=>31,8=>31,9=>30,10=>31,11=>30,12=>31);
   if($year%100 == 0)
   {
       if($year%400 == 0)
         $day[$month] = 29;
   }
   else if($year%4 == 0)
       $day[$month] = 29;

   return "{$year}-{$month}-{$day[$month]}";
}

Upvotes: 0

ziad-saab
ziad-saab

Reputation: 20269

You want to replace your date() call with:

date('Y-m-t', strtotime($year.'-'.$month.'-01'));

The first parameter to date() is the format you want to be returned, and the second parameter has to be a unix timestamp (or not passed to use the current timestamp). In your case, you can generate a timestamp with the function strtotime(), passing it a date string with the year, the month, and 01 for the day. It will return that same year and month, but the -t in the format will be replaced by the last day of the month.

If you want to return only the last day of the month without year and month:

date('t', strtotime($year.'-'.$month.'-01'));

Just use 't' as your format string.

Upvotes: 2

deceze
deceze

Reputation: 522530

Current month:

echo date('Y-m-t');

Any month:

echo date('Y-m-t', strtotime("$year-$month-1"));

Upvotes: 1

Laurence
Laurence

Reputation: 60068

date("Y-m-d",strtotime("-1 day" ,strtotime("+1 month",strtotime(date("m")."-01-".date("Y")))));

Upvotes: 0

Naresh Tank
Naresh Tank

Reputation: 1568

    function firstOfMonth() {
return date("Y-m-d", strtotime(date('m').'/01/'.date('Y').' 00:00:00')). 'T00:00:00';}

function lastOfMonth() {
return date("Y-m-d", strtotime('-1 second',strtotime('+1 month',strtotime(date('m').'/01/'.date('Y').' 00:00:00')))). 'T23:59:59';}

$date1 = firstOfMonth();
$date2  = lastOfMonth();

try this ,this give you a current month's starting and ending date.

Upvotes: 0

Hampus Brynolf
Hampus Brynolf

Reputation: 1336

This code will give you last day for a specific month.

$datetocheck = "2012-03-01";
$lastday = date('t',strtotime($datetocheck));

Upvotes: 4

danish hashmi
danish hashmi

Reputation: 484

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);
}

Upvotes: 0

pinaldesai
pinaldesai

Reputation: 1815

Try below code.

$m = '03';//
$y = '2012'; //

$first_date = date('Y-m-d',mktime(0, 0, 0, $m , 1, $y));

$last_day   = date('t',strtotime($first_date));
$last_date = date('Y-m-d',mktime(0, 0, 0, $m ,$last_day, $y));

Upvotes: 0

Related Questions