2015-10-15 100 views
1

我建立多日曆,我有一個水平看接口: enter image description here環月份的天遍及整個月

我想在整個一月運行一週的日子S,M,T,W,T,F,S 代替就像圖片中的前7個一樣。

其繪製日曆功能:

//our case "SUN" 
if(AC_START_DAY=="sun"){ 
    for($k=0; $k<7; $k++){ 
     $weekday = mb_substr($lang["day_".$k.""],0,1,'UTF-8'); 
     $list_day_titles.='<li class="cal_weekday"> '.$weekday.'</li>'; 
    } 
} 
//If we chose Monday as start week. 
else{ 
    if ($first_week_day == 0) $first_week_day =7; 
    for($k=1; $k<=7; $k++){ 
     if($k==7) $weekday = mb_substr($lang["day_0"][0],0,1,'UTF-8'); 
     else  $weekday = mb_substr($lang["day_".$k.""],0,1,'UTF-8'); 
     $list_day_titles.='<li title="'.$lang["day_".$k.""].'"> '.$weekday.'</li>'; 
    } 
} 

朗文件:

$lang["day_0"]  = "Sunday"; 
$lang["day_1"]  = "Monday"; 
$lang["day_2"]  = "Tuesday"; 
$lang["day_3"]  = "Wednesday"; 
$lang["day_4"]  = "Thursday"; 
$lang["day_5"]  = "Friday"; 
$lang["day_6"]  = "Saturday"; 

已經定義

$month=sprintf("%02s",$month); 
// define vars 
$today_timestamp = mktime(0,0,0,date('m'),date('d'),date('Y')); # current timestamp - used to check if date is in past 
$this_month   = getDate(mktime(0, 0, 0, $month, 1, $year));  # convert month to timestamp 
$first_week_day  = $this_month["wday"];        # define first weekday (0-6) 
$days_in_this_month = cal_days_in_month(CAL_GREGORIAN,$month,$year); # define number of days in week 
$day_counter_tot = 0; # count total number of days showin INCLUDING previous and next months - use to get 6th row of dates 

貌似$lang["day_".$k.""]只是數着日子從0到6 ..我怎麼能循環直到月底?

注:我試圖增加$k<7只是出現更多空的藍色框。

+0

好開始使用這些功能將需要知道他們正在處理什麼一個月,所以它可以計算出可天有當月,這樣你就可以改變環路限制爲正確的數字的天輸出 – RiggsFolly

回答

0

使用循環到30/31天。

然後改變這一行

$weekday = mb_substr($lang["day_".$k.""],0,1,'UTF-8'); 

$weekday = mb_substr($lang["day_".$k%7.""],0,1,'UTF-8'); 

這應該給你0天每個星期天。

0 % 7 = 0 (sunday) 
1 % 7 = 1 (monday) 
... 
7 % 7 = 0 (sunday again) 
8 % 7 = 1 (monday again) 
+0

這是錯誤的語法..沒有加載 –

0

您可以使用此代碼生成當前月份的所有天數。

for ($date = strtotime(date('Y-m-01')); $date < strtotime(date('Y-m-t')); $date = strtotime("+1 day", $date)) { 
     echo date("l-d", $date)."<br>"; 
    } 

將打印當前月份的所有日期,如下所示。

Thursday-01 
Friday-02 
Saturday-03 
Sunday-04 
Monday-05 
Tuesday-06 
Wednesday-07 
Thursday-08 
Friday-09 
Saturday-10 
Sunday-11 
Monday-12 
Tuesday-13 
Wednesday-14 
Thursday-15 
Friday-16 
Saturday-17 
Sunday-18 
Monday-19 
Tuesday-20 
Wednesday-21 
Thursday-22 
Friday-23 
Saturday-24 
Sunday-25 
Monday-26 
Tuesday-27 
Wednesday-28 
Thursday-29 
Friday-30 
0

看起來你幾乎是對的。 您只需稍微修改您的代碼即可使其以您希望的方式工作。 你應該只改變你的代碼:

$number_of_days_in_the_future = 42; // Here you can put in the number of days for which you want to display the corresponding letter, and based on your screenshot that is 42 
//our case "SUN" 
if(AC_START_DAY=="sun"){ 
    for($i=0; $i<$number_of_days_in_the_future; $i++){ 
     $k = $i % 7; 
     $weekday = mb_substr($lang["day_".$k.""],0,1,'UTF-8'); 
     $list_day_titles.='<li class="cal_weekday"> '.$weekday.'</li>'; 
    } 
} 
//If we chose Monday as start week. 
else{ 
    if ($first_week_day == 0) $first_week_day =7; 
    for($i=1; $i<=$number_of_days_in_the_future; $i++){ 
     $k = $i % 7; 
     if($k==7) $weekday = mb_substr($lang["day_0"][0],0,1,'UTF-8'); 
     else  $weekday = mb_substr($lang["day_".$k.""],0,1,'UTF-8'); 
     $list_day_titles.='<li title="'.$lang["day_".$k.""].'"> '.$weekday.'</li>'; 
    } 
} 

請注意,我只是試圖修復您的代碼,以便它可以作爲你指望它。 有可能是一個更優雅的解決方案,但我不知道你的完整代碼,所以我只是猜測,如果我試圖給你另一種方法。

我希望這會幫助你。

乾杯