我需要做一個for循環是循環的月份數。 但它應該在for循環中扣除月份值。當1月來到它應該回到12獲取php日期for循環
for($i=0;$i>4;++$i){ echo $month = date('m') - $i; }
由此可見0。
我需要做一個for循環是循環的月份數。 但它應該在for循環中扣除月份值。當1月來到它應該回到12獲取php日期for循環
for($i=0;$i>4;++$i){ echo $month = date('m') - $i; }
由此可見0。
好的。我發現了一個解決方案:
for($i=0;$i<4;++$i){
$new_month = date("m", mktime(0, 0, 0, date("m") - $i, 1, date("Y")));
}
<?php
$start = $month = strtotime('2013-02-01');
$endmonth = strtotime('2014-01-01');
while($month < $endmonth)
{
echo date('F Y', $month), PHP_EOL;
echo "<br />";
$month = strtotime("+1 month", $month);
}
?>
試試這個這個循環中,您還可以得到每月
這是計算所需的輸出純數學方法:
$startMonth = (int)date('m');
for($i=0; $i<30; $i++) {
$echoMonth = $startMonth - $i;
if ($echoMonth <= 0) {
echo (12 + ($echoMonth % 12));
} else {
echo $echoMonth;
}
echo "\n";
}
的% - 運算符是取模功能,它保留你從0到11範圍內減去的數字,這樣你可以保持你的數字在1到12的範圍內。
希望這就是你要找的。
這個循環將無法正常工作,甚至曾經爲($ I = 0; $ I> 4; ++ $ I) –
多少次迭代'for'循環? – hindmost
錯誤..它倒轉了。 for($ i = 0; $ i <4; ++ $ i) – Mad