11

How to get last day of the month using php ?

<?php
     echo date('?-?-?');
?>
hsuk
  • 6,770
  • 13
  • 50
  • 80
jems peterson
  • 147
  • 1
  • 2
  • 7

6 Answers6

10
<?php
$day=new DateTime('last day of this month'); 
echo $day->format('M jS');
?>
Mayur Patel
  • 245
  • 1
  • 12
9

The date function documentation says that t represents number of days in current month:

$day = date( 't-m-Y' );
hjpotter92
  • 78,589
  • 36
  • 144
  • 183
8

try this

$day=date('Y-m-t'); // gives last day of current month

OR

$d = new DateTime( '2013-05-03' ); 
echo $d->format( 'Y-m-t' );
Yogesh Suthar
  • 30,424
  • 18
  • 72
  • 100
4

Try:

$last_day = date('t-m-Y');

where t means the last date of the current month.

PHP: date - Manual

How can I find the first and last date in a month using PHP?

Community
  • 1
  • 1
hsuk
  • 6,770
  • 13
  • 50
  • 80
1

Using the function date you would do

$day = date("t");

Please read the documentation

MISJHA
  • 998
  • 4
  • 12
1

Try this

$date = new DateTime();
$lastDayOfMonth = $date->modify(
  sprintf('+%d days', $date->format('t') - $date->format('j'))
);
Santosh Panda
  • 7,235
  • 8
  • 43
  • 56