Reputation: 153
I am looking to list every year between dates using PHP and Mysql data base, the code is this:
<?php
$start = new DateTime('2010-12-02');
$start->modify('first day of this month');
$end = new DateTime('2016-05-06');
$end->modify('first day of next month');
$interval = DateInterval::createFromDateString('1 year');
$period = new DatePeriod($start, $interval, $end);
foreach ($period as $dt) {
echo $dt->format("Y") . "<br>\n";
}
?>
Output
2010
2011
2012
2013
2014
2015
Missing the year 2016.
How I can do to make 2016 also returns? It should return as it does to 2010
Upvotes: 3
Views: 1717
Reputation: 9
<?php
$start = new DateTime('2010-12-02')->modify('+1 year');
$start->modify('first day of this month');
$end = new DateTime('2016-05-06');
$end->modify('first day of next month');
$interval = DateInterval::createFromDateString('1 year');
$period = new DatePeriod($start, $interval, $end);
foreach ($period as $dt) {
echo $dt->format("Y") . "<br>\n";
}
?>
Upvotes: 0
Reputation: 4013
It is simple: you are iterating between 2010-12-02
and 2016-05-06
. The generated dates are:
2010-12-02
-> ok2011-12-02
-> ok2012-12-02
-> ok2013-12-02
-> ok2014-12-02
-> ok2015-12-02
-> ok2016-12-02
-> over 2016-05-06
- stop algorithm.I do not know exactly what do you want to do, but it seem that you should change end date.
Upvotes: 0
Reputation: 2683
$start = '2010-12-02';
$end = '2016-05-06';
$getRangeYear = range(gmdate('Y', strtotime($start)), gmdate('Y', strtotime($end)));
print_r($getRangeYear);
use the range function to get list of ranging numbers, first parameter start range, and second ending range.. http://php.net/manual/en/function.range.php
Upvotes: 1