How to set the time interval through PHP?

I put this code. Returns Calvi a month with an interval of one day. As if the goal is achieved, but not missing the last day of the month. At this March, only 31 days, and gives 30. How do I properly set the date that he showed me all the days of the month?

$begin = new \DateTime(date('Y-m-01'));
 $interval = new \DateInterval('P1D');
 $end_date = date('Y-m-') . date('t');
 $end = new \DateTime($end_date);
 $date = new \DatePeriod($begin,$interval, $end);
September 19th 19 at 00:04
3 answers
September 19th 19 at 00:06
Solution
$begin = new \DateTime(date('Y-m-01'));
 $interval = new \DateInterval('P1D');
 $end_date = date('Y-m-') . date('t');
 $end = new \DateTime($end_date);
$end->setTime(23,59,59);
 $date = new \DatePeriod($begin,$interval, $end);

The option native to the calculation date, a bit more compact and better readable:
$begin = new \DateTime('first day of this month');
 $interval = new \DateInterval('P1D');
 $end = new \DateTime('last day of this month');
$end->setTime(23,59,59);
 $date = new \DatePeriod($begin,$interval, $end);


Or not theoretically beautiful, but strange without backup:
for ($d = clone $begin; $d <= $end; $d->modify('next day midnight')) {
 echo $d->format('Y-m-d'),PHP_EOL;
 }
"The native calculation date, a little more compact and readable the better:" Thank you it helped!) - Franz.Waters16 commented on September 19th 19 at 00:09
September 19th 19 at 00:08
Muahaha =)
$end_date = date('Y-m-t');
And where still 30 days shows =) I tried adding +1, it gives ERROR that there can be 32 days this month! - Franz.Waters16 commented on September 19th 19 at 00:11
ok.
$date = date('t')
Here you have the number of days in the current month. - Franz.Waters16 commented on September 19th 19 at 00:14
September 19th 19 at 00:10
$end_date = date('Y-m-') . date('t'); // ???
$end_date = date('Y-m-t');

$begin = new \DateTime(date('Y-m-00'));
$end = new \DateTime(date('Y-m-t'));

var_dump($begin->diff($end)->days); // int(31)

Find more questions by tags PHP