问题
is there a way I can use a PHP loop to create a list like the following with the previous 12 months, based on the current month (excluding the current month) ? The value should always be the first of the month (format: yyyy-mm-dd) and the dropdown itself should just show year and month (format: yyyy-mm):
<option value="2014-03-01">2014-03</option>
<option value="2014-02-01">2014-02</option>
<option value="2014-01-01">2014-01</option>
<option value="2013-12-01">2013-12</option>
<option value="2013-11-01">2013-11</option>
<option value="2013-10-01">2013-10</option>
//...
I tried the following but seem to have something wrong there as this is not working:
<?php for ($i=0; $i<=12; $i++) { ?>
<option value="<?php echo date('Y-m-d', strtotime("-1 month")); ?>"><?php echo date('Y-m', strtotime("-1 month")); ?></option>
<? } ?>
Many thanks for any help with this, Tim.
回答1:
<?php
for ($i=0; $i<=12; $i++) {
echo '<option value="'.date('Y-m-d', strtotime("-$i month")).'">'.date('Y-m', strtotime("-$i month")).'</option>';
}
回答2:
$start = (new DateTime('1 year ago'))->modify('first day of this month');
$end = (new DateTime())->modify('first day of this month');
$interval = new DateInterval('P1M');
$period = new DatePeriod($start, $interval, $end);
$months = array();
foreach ($period as $dt) {
$months[$dt->format('Y-m-d')] = $dt->format('Y-m');
}
$reverse_months = array_reverse($months);
print_r($reverse_months);
Demo
You can then loop through $reverse_months to create your dropdown
foreach($reverse_months as $key => $value) {
?>
<option value="<?php echo key; ?>"><?php echo value; ?></option>
<?php
}
The reason why we have to use array_reverse()
is DatePeriod only goes forward in time.
回答3:
You can use the datetime extension to create an iterator. It is quite simple:
// This is when to start counting
$start = new DateTime('now');
// The interval; i.e. every time we iterate we should get
// the first day of the previous month
$interval = DateInterval::createFromDateString('first day of last month');
// The period (or the iterator) should go for twelve
// months and the start date should not be included
$period = new DatePeriod($start, $interval, 12, DatePeriod::EXCLUDE_START_DATE);
// The DatePeriod class implements the Traversable
// interface and can therefore be used in a foreach loop
foreach($period as $time) {
$val = $time->format("Y-m-d");
$txt = $time->format("Y-m");
echo "<option value=\"{$val}\">{$txt}</option>\n";
}
回答4:
This is what I have used, it's a bit rudimentary but works even when you get to the end of the month .. unlike the accepted answer as Glavić pointed out:
<?php
$year = date("Y");
$month = date("m");
for ($i=0; $i<=12; $i++)
{
$month--;
if ($month < 1) { $month = 12; $year--; }
echo '<option value="'.date('Y-m-d', strtotime($year."-".$month."-01")).'">'.date('Y-m', strtotime($year."-".$month."-01")).'</option>';
}
回答5:
Change -1 to $i
to reflect and store it for double use.
<?php
for ($i=1; $i>=12; $i++) {
$last = strtotime("-$i month");
?>
<option value="<?php echo date('Y-m-d', $last);?>"><?php echo date('Y-m', $last);?></option>
<? } ?>
来源:https://stackoverflow.com/questions/22847579/php-loop-to-create-list-of-previous-12-months