2015-10-12 120 views
-1

我很安靜....我仍然搞亂了很多代碼。顯示年份日曆

我做了一個數組:

$months = array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"); 

然後,我有一個返回的天數每月switch語句:

function month_length($month) { 

    $leap_year = date("L"); 

    switch ($month) { 
     case "January": return $max = 31;  
     case "February": 
       if ($leap_year === 1) { 
       return $max = 29; 
      } else { 
       return $max = 28; 
      } 
     case "March":  return $max = 31; 
     case "April":  return $max = 30; 
     case "May":  return $max = 31; 
     case "June":  return $max = 30; 
     case "July":  return $max = 31; 
     case "August":  return $max = 31; 
     case "September": return $max = 30; 
     case "October": return $max = 31; 
     case "November": return $max = 30; 
     case "December": return $max = 31; 
     default:   return $max = "wrong number"; 
    }  
} 

有什麼辦法,如果任何我可以比較數組的對象與任何情況相匹配?

讓我澄清;所以目前,我需要設置一個$ current_month讓php知道我想要顯示的情況,並且我希望能夠顯示每年的所有月份。

有誰知道我在說什麼?所以我想我需要比較數組月的字符串和switch語句的情況,但是......怎麼樣?因爲我不想每個都要比較一個:S

謝謝!!!!

+0

你的意思是像['foreach'](http://php.net/manual/en/control-structures.foreach.php)? –

+0

[date()](http://php.net/manual/en/function.date.php)函數可能對您有很大的幫助。 – castis

+0

mmmm我不這麼認爲......儘管我不太瞭解。如何循環使用foreach來檢查值是否匹配? :我的上帝,這太難了!讓我加入jsfiddle! –

回答

1

一個簡單的解決方案與foreach

foreach($months as $month) { 
    echo "The month with name $month has ".month_length($month)." days.<br>"; 
} 
+0

:它不會在月份工作....在這裏檢查,看看我做了什麼。可能我做了一個巨大的錯誤......但我看不到它! http://phpfiddle.org/main/code/bc3fdcb7806a982b2982 –

+0

arg sorrry我剛剛明白了! 謝謝! –

0

此代碼將打印表日曆。它將顯示5列每行7天。從30/31到35的日子裏充滿了該月的下一天或前幾天以保持周的方面。

希望它有幫助。

function renderMonth($displayM, $displayY){ 

     $daysOfWeek = array (
       'Mon', 
       'Tue', 
       'Wen', 
       'Thu', 
       'Fri', 
       'Sat', 
       'Sun' 
     ); 

     $dateUtil = new DateTime ($displayY . "/" . $displayM . "/01" ); 
     $year = $dateUtil->format ("Y"); 
     $week = $dateUtil->format ("W"); 

     $week_start = new DateTime(); 
     $week_start->setISODate ($year, $week); 
     $nextDay = clone $week_start; 

     $i = 1; 
     $weekdays = 7; // how many days do we display per row 
     $currday = 1; // current week day 
     $daysno = 35; // number of display dates 

     $calendar = '<table class="event-calendar">'; 
     $calendar .= "<thead>"; 
     $calendar .= "<tr>"; 
     foreach ($daysOfWeek as $day) { 
      $calendar .= "<th>$day</th>"; 
     } 
     $calendar .= "</tr>"; 
     $calendar .= "</thead>"; 
     $calendar .= "<tbody>"; 
     while ($i < $daysno) { 
      if ($i == 1) { 
       $calendar .= '<tr>'; 
       $calendar .= '<td>' . $nextDay->format ('d M') . '</td>'; 
      } 
      $currday ++; 
      if ($currday > $weekdays) { 
       $calendar .= '</tr>'; 
       $calendar .= '<tr>'; 
       $currday = 1; 
      } 
      $nextDay->add (new DateInterval ('P1D')); 
      $calendar .= '<td>' . $nextDay->format ('d M') . '</td>'; 
      $i ++; 
      if ($i == $daysno) { 
       $calendar .= '</tr>'; 
      } 
     } 
     $calendar .= "</tbody>"; 
     $calendar .= "</table>"; 
     return $calendar; 
    } 


    echo renderMonth("10", "2015"); 
    echo renderMonth("11", "2015"); 
    echo renderMonth("12", "2015"); 
相關問題