PHP

[ Solved -8 Answers] How to find the last day of the month from date

How to find the last day of the month from date- t returns the number of days in the month of a given date

How can we get the last day of the month in PHP?

Given:

Php code
$a_date = "2017-01-23“

We want 2017-01-30; and given

$a_date = "2017-02-23“

We  want 2017-02-31.

php date

  • t returns the number of days in the month of a given date (see the docs for date):
Php code
$a_date = "2009-11-23";
echo date("Y-m-t", strtotime($a_date));

  • The code using strtotime() will fail after year 2038. (as given in the first answer in this thread) For example try using the following:
javascript code
$a_date = "2040-11-23";
echo date("Y-m-t", strtotime($a_date));
  • It will give answer as: 1970-01-31
  • So instead of strtotime, DateTime function should be used. Following code will work without Year 2038 problem:
Php code
$d = new DateTime( '2040-11-23' ); 
echo $d->format( 'Y-m-t' );

  • “This function will return the number of days in the month of year for the specified calendar.
  • ” http://php.net/manual/en/function.cal-days-in-month.
php code
echo cal_days_in_month(CAL_GREGORIAN, 11, 2009); 
// = 30

This should work:

Php code
$week_start = strtotime('last Sunday', time());
$week_end = strtotime('next Sunday', time());

$month_start = strtotime('first day of this month', time());
$month_end = strtotime('last day of this month', time());

$year_start = strtotime('first day of January', time());
$year_end = strtotime('last day of December', time());

echo date('D, M jS Y', $week_start).'<br/>';
echo date('D, M jS Y', $week_end).'<br/>';

echo date('D, M jS Y', $month_start).'<br/>';
echo date('D, M jS Y', $month_end).'<br/>';

echo date('D, M jS Y', $year_start).'<br/>';
echo date('D, M jS Y', $year_end).'<br/>';

  • You could create a date for the first of the next month, and then use strtotime(“-1 day”, $firstOfNextMonth)

  • If you use the Carbon API extension for PHP DateTime, you can get the last day of the month with:
Php code
$date = Carbon::now();
$date->addMonth();
$date->day = 0;
echo $date->toDateString(); // use toDateTimeString() to get date and time 

  • Try this , if you are using PHP 5.3+,
Php code
$a_date = "2009-11-23";
$date = new DateTime($a_date);
$date->modify('last day of this month');
echo $date->format('Y-m-d');
  • For finding next month last date, modify as follows,
Php code
$date->modify('last day of 1 month');
 echo $date->format('Y-m-d');

  • Here is a complete function:
Php code
public function get_number_of_days_in_month($month, $year) 
{
    // Using first day of the month, it doesn't really matter
    $date = $year."-".$month."-1";
    return date("t", strtotime($date));
}
  • This would output following:
php code
echo get_number_of_days_in_month(2,2014);

Output:

28

READ  [Solved - 8 Answers] PHP - How to secure database passwords in PHP

About the author

Venkatesan Prabu

Venkatesan Prabu

Wikitechy Founder, Author, International Speaker, and Job Consultant. My role as the CEO of Wikitechy, I help businesses build their next generation digital platforms and help with their product innovation and growth strategy. I'm a frequent speaker at tech conferences and events.

Add Comment

Click here to post a comment

X