Reputation: 11812
I am trying to display a date range for each month.
For example,
November 2013 -- 2013-11-01 - 2013-11-30 //for month Nov
December 2013 -- 2013-12-01 - 2013-12-31 //for month Dec
January 2013 -- 2014-01-01 - 2014-01-31 //for month jan 2014
so on till dec 2014
PHP code:
$currentmonth = date('n');
$monthstoloop = $currentmonth + 13;
for ($m=$currentmonth; $m<=$monthstoloop; $m++) {
$month = date('F', mktime(0,0,0,$m, 1, date('Y')));
$year = date('Y', mktime(0,0,0,$m, 1, date('Y')));
echo $month." ".$year;
}
I am not finding a way to display the month range just beside the year.Please suggest.
Upvotes: 0
Views: 423
Reputation: 43552
Use format parameter t
to get last day of the month:
$start = new DateTime('2013-10-01');
$end = new DateTime('2014-05-01');
while ($start < $end) {
echo $start->format('M Y = Y-m-d - Y-m-t'), "\n";
$start->modify('+1 month');
}
Demo.
Updated your example:
$currentmonth = date('n');
$monthstoloop = $currentmonth + 13;
for ($m=$currentmonth; $m<=$monthstoloop; $m++) {
$time = mktime(0,0,0,$m,1,date('Y'));
echo date('F Y = Y-m-d - Y-m-t', $time), "\n";
}
Upvotes: 1
Reputation: 76656
It's easier with DateTime class:
$start = new DateTime('2013-11-01');
$end = new DateTime('2014-12-01');
while ($start <= $end) {
echo $start->format('F Y -- Y-m-d - Y-m-t').'<br/>';
$start->modify('+1 month');
}
Output:
November 2013 -- 2013-11-01 - 2013-11-30
December 2013 -- 2013-12-01 - 2013-12-31
January 2014 -- 2014-01-01 - 2014-01-31
February 2014 -- 2014-02-01 - 2014-02-28
March 2014 -- 2014-03-01 - 2014-03-31
April 2014 -- 2014-04-01 - 2014-04-30
May 2014 -- 2014-05-01 - 2014-05-31
June 2014 -- 2014-06-01 - 2014-06-30
July 2014 -- 2014-07-01 - 2014-07-31
August 2014 -- 2014-08-01 - 2014-08-31
September 2014 -- 2014-09-01 - 2014-09-30
October 2014 -- 2014-10-01 - 2014-10-31
November 2014 -- 2014-11-01 - 2014-11-30
December 2014 -- 2014-12-01 - 2014-12-31
Upvotes: 1
Reputation: 60007
There are only 12 months in a year.
Why not hard code them and have the fiddle with February
Seems an easy and quick solution
Upvotes: 0