0

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';
Question User
  • 2,093
  • 3
  • 19
  • 29
  • rtfm: http://php.net/date if you DON'T provide a timestamp for date() as the second argument, it uses TODAY. therefore you're generating a `t` value for August, not June. – Marc B Aug 04 '16 at 14:51
  • Possible duplicate of [How to find the last day of the month from date?](http://stackoverflow.com/questions/1686724/how-to-find-the-last-day-of-the-month-from-date) – andreas Aug 04 '16 at 14:54
  • this is not a duplicate question. just check my question @Andreas – Question User Aug 04 '16 at 14:57
  • how can i overcome with this issue @MarcB ? – Question User Aug 04 '16 at 14:57

1 Answers1

4

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);
VoteyDisciple
  • 37,319
  • 5
  • 97
  • 97
  • i need to get the last day of the month not month number – Question User Aug 04 '16 at 14:56
  • The `$month_number ` is from your original code. Do you mean you want ***just*** the month number and not the last day of the month, in addition to `$month_number`, `$calcstart_date` and `$calcend_date`? – VoteyDisciple Aug 04 '16 at 14:57