11

How to get last day of the month using php ?

<?php
     echo date('?-?-?');
?>
0

6 Answers 6

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

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

$day = date( 't-m-Y' );
1
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' );
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?

1

Using the function date you would do

$day = date("t");

Please read the documentation

1

Try this

$date = new DateTime();
$lastDayOfMonth = $date->modify(
  sprintf('+%d days', $date->format('t') - $date->format('j'))
);

Not the answer you're looking for? Browse other questions tagged or ask your own question.