我需要根据当前月份显示月份。因此,如果是 7 月,我只想返回接下来的 4 个月,例如“八月”、“九月”、“十月”和“十一月”。
我怎样才能做到这一点?
$mnth = date('F');
$mnth_arr = array("january","febuary","march","april","may","june","july","august","september","october","november","december");
我需要根据当前月份显示月份。因此,如果是 7 月,我只想返回接下来的 4 个月,例如“八月”、“九月”、“十月”和“十一月”。
我怎样才能做到这一点?
$mnth = date('F');
$mnth_arr = array("january","febuary","march","april","may","june","july","august","september","october","november","december");
这对于DateTime类来说非常简单:-
$now = new \DateTime();
$interval = new \DateInterval('P1M');
$period = new \DatePeriod($now->add($interval), $interval, 4);
foreach($period as $date){
echo $date->format('F') . "<br/>";
}
输出:-
八月
九月
十月
十一月
十二月
$key = array_search($mnth, $mnth_arr);
for($key; $key<$key+4; $key++)
{
echo $mnth_arr[$key];
}
您可以使用带有相对偏移量的 strtotime 来完成此操作。
// get the next four months.
$ref = strtotime("+1 month");
for($x =0; $x<4; $x++){
echo date("F", $ref)."\n";
$ref = strtotime("+1 month", $ref);
}