Question User Question User - 4 months ago 12
PHP Question

how to get the last day of the month using php

I'm trying to get the last day from the month using

strtotime
.I don't know what wrong with the below code. Can some one shoot out the issue ?

$string = 'Jun';
$month_number = date("m",strtotime($string));
$calcstart_date = date('Y-'.$month_number.'-01').' 00:00:00';
echo $calcend_date = date('Y-'.$month_number.'-t').' 11:59:00';

Answer

Your last two date() calls aren't based in any way on the timestamp you computed earlier; those will just use the number of days in the current month.

You want:

$time = strtotime($string);
$calcstart_date = date('Y-m-\0\1', $time);
$calcend_date = date('Y-m-t', $time);

And if you need the month number independently you can still do

$month_number = date('m', $time);

Finally if you'd like just the last day number by itself, that would be:

$number_of_days = date('t', $time);