2013-11-09 132 views
0

我想顯示每個月的日期範圍。顯示每個月的日期範圍

例如,

2013年11月 - 2013年11月1日 - 二○一三年十一月三十○日//爲一個月十一月

2013年12月 - 2013年12月1日 - 2013-12-31 //爲月月

2013年1月 - 2014年1月1日 - 2014-01-31 //爲月2014年1月

以此類推,直到2014年12月

PHP代碼:

$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; 
} 

我沒有找到一種方式來顯示年份旁邊的月份範圍。請提示。

回答

1

使用format參數t拿到這個月的最後一天:

$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


更新您的例子:

$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"; 
} 
+1

輝煌。謝謝Glavic。 –

+1

'+ 1'實際回答了這個問題:) –

+0

PrithvirajMitra:np,很高興我能幫上忙。 @AmalMurali:我看到我們覺得一樣,我們回答的答案相同;) –

0

有一年只有12個月。

爲什麼不硬編碼它們並有小提琴,今年2月

似乎是一個簡單而快速的解決方案

1

這是一個與DateTime類簡單:

$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'); 
} 

輸出:

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 

Demo!

+0

不錯的一個Amal,但由於某種原因,我不能使用日期時間類。非常感謝您的努力。 –

+0

@PrithvirajMitra:無賴。無論如何,祝你的項目好運。 :) –