2016-11-10 56 views
0

嗨我試圖製作一個日曆表,顯示一年中的月份和一週中的幾天。PHP日曆表

我能夠在這個月做一個(2016年11月)。現在我想循環通過一年和未來幾年。

有人可以幫助我嗎?

<?php 
/* Set the default timezone */ 
date_default_timezone_set("Asia/Hong_Kong"); 

/* Set the date */ 
$date = strtotime(date("Y-m-d")); 

$day = date('d', $date); 
$month = date('m', $date); 
$year = date('Y', $date); 

// $nextyear = strtotime('+1 month', $date); 

$firstDay = mktime(0,0,0,$month, 1, $year); 
$title = strftime('%B', $firstDay); 
$dayOfWeek = date('D', $firstDay); 
$daysInMonth = cal_days_in_month(0, $month, $year); 
/* Get the name of the week days */ 
$timestamp = strtotime('next Sunday'); 
$weekDays = array(); 



for ($i = 0; $i < 32; $i++) { 
    $weekDays[] = strftime('%a', $timestamp); 
    $timestamp = strtotime('+1 day', $timestamp); 
} 
$blank = date('w', strtotime("{$year}-{$month}-01")); 
?> 




<table class='table table-bordered' style="table-layout: fixed;"> 
    <tr> 
     <th colspan="32" class="text-center"> <?php echo $title ?> <?php echo $year ?> </th> 
    </tr> 
    <tr> 
     <?php foreach($weekDays as $key => $weekDay) : ?> 
      <td class="text-center"><?php echo $weekDay ?></td> 
     <?php endforeach ?> 
    </tr> 
    <tr> 
     <?php for($i = 0; $i < $blank; $i++): ?> 
      <td></td> 
     <?php endfor; ?> 
     <?php for($i = 1; $i <= $daysInMonth; $i++): ?> 
      <?php if($day == $i): ?> 
       <td><strong><?php echo $i ?></strong></td> 
      <?php else: ?> 
       <td><?php echo $i ?></td> 
      <?php endif; ?> 
      <?php if(($i + $blank) % 32 == 0): ?> 
       </tr><tr> 
      <?php endif; ?> 
     <?php endfor; ?> 
     <?php for($i = 0; ($i + $blank + $daysInMonth) % 32 != 0; $i++): ?> 
      <td></td> 
     <?php endfor; ?> 
    </tr> 
</table> 
+1

你爲什麼想這麼做?這很痛苦!看看客戶端的JavaScript庫,而不是。他們在那裏,準備好使用,提供廣泛的功能,選項和主題。 – arkascha

+0

我已經檢查了其中的一些,但我需要的只是一個簡單的日曆表,它顯示一個整月的horinzontically不像典型的日曆表 –

+0

啊,好吧,我明白了。所以你說你有一個月,現在想循環幾個月?好的,那是什麼問題?爲什麼你不能簡單地循環?無論如何,你需要一些目錄來編碼每月的天數。 – arkascha

回答

2

正如在上面的評論中所說的,你可以使用兩個循環嵌套在一起,一個爲年,一個爲月。這樣你就可以根據不同的日期執行你已經給出的代碼,但是當前的代碼。

看看這種方法。它基本上你在找什麼,但你需要做一些微調,佈局:

<?php 

define('NUMBER_OF_COLUMNS', 37);  

function renderCalenderMonth($date) { 
    $day = date('d', $date); 
    $month = date('m', $date); 
    $year = date('Y', $date); 

    $firstDay = mktime(0,0,0,$month, 1, $year); 
    $title = strftime('%B', $firstDay); 
    $dayOfWeek = date('D', $firstDay); 
    $daysInMonth = cal_days_in_month(0, $month, $year); 
    /* Get the name of the week days */ 
    $timestamp = strtotime('next Sunday'); 
    $weekDays = array(); 

    for ($i = 0; $i < NUMBER_OF_COLUMNS; $i++) { 
     $weekDays[] = strftime('%a', $timestamp); 
     $timestamp = strtotime('+1 day', $timestamp); 
    } 
    $blank = date('w', strtotime("{$year}-{$month}-01")); 
    ?> 

    <table class='table table-bordered' style="table-layout: fixed;"> 
     <tr> 
     <th colspan="<?php echo NUMBER_OF_COLUMNS?>" class="text-center"> <?php echo $title ?> <?php echo $year ?> </th> 
     </tr> 
     <tr> 
     <?php foreach($weekDays as $key => $weekDay) : ?> 
      <td class="text-center"><?php echo $weekDay ?></td> 
     <?php endforeach ?> 
     </tr> 
     <tr> 
     <?php for($i = 0; $i < $blank; $i++): ?> 
      <td></td> 
     <?php endfor; ?> 
     <?php for($i = 1; $i <= $daysInMonth; $i++): ?> 
      <?php if($day == $i): ?> 
      <td><strong><?php echo $i ?></strong></td> 
      <?php else: ?> 
      <td><?php echo $i ?></td> 
      <?php endif; ?> 
      <?php if(($i + $blank) % NUMBER_OF_COLUMNS == 0): ?> 
      </tr><tr> 
      <?php endif; ?> 
     <?php endfor; ?> 
     <?php for($i = 0; ($i + $blank + $daysInMonth) % NUMBER_OF_COLUMNS != 0; $i++): ?> 
      <td></td> 
     <?php endfor; ?> 
     </tr> 
    </table> 

    <?php 
} 

// =========================== 

/* Set the default timezone */ 
date_default_timezone_set("Asia/Hong_Kong"); 

for ($iterateYear=2016; $iterateYear<2018; $iterateYear++) { 
    for ($iterateMonth=1; $iterateMonth<=12; $iterateMonth++) { 

    /* Set the date */ 
    $date = strtotime(sprintf('%s-%s-01', $iterateYear, $iterateMonth)); 
    renderCalenderMonth($date); 

    } 
} 
+0

嗨我目前正在將代碼遷移到laravel 5.3我發佈了我的laravel 5.3代碼。你能幫我檢查一下嗎?它沒有循環 –

0

Laravel 5.3版本的日曆代碼:

public function index() 
{ 
    $users = DB::table('users')->where('roles_id', '2')->get(); 

    date_default_timezone_set("Asia/Hong_Kong"); 

    for ($iterateYear = 2017; $iterateYear < 2018; $iterateYear++) { 
     for ($iterateMonth = 1; $iterateMonth <= 12; $iterateMonth++) { 
      /* Set the date */ 
      $date = strtotime(sprintf('%s-%s-01', $iterateYear, $iterateMonth)); 
      return $this->renderCalenderMonth($date); 
     } 
    } 

    return view('transaction.index', [ 
     'users' => $users 
    ]); 
} 

public function renderCalenderMonth($date) 
{ 
    // $date = strtotime(date("Y-m-d")); 
    $NUMBER_OF_COLUMNS = 37; 

    $day = date('d', $date); 
    $month = date('m', $date); 
    $year = date('Y', $date); 

    $firstDay = mktime(0,0,0,$month, 1, $year); 
    $title = strftime('%B', $firstDay); 
    $dayOfWeek = date('D', $firstDay); 
    $daysInMonth = cal_days_in_month(0, $month, $year); 
    /* Get the name of the week days */ 
    $timestamp = strtotime('next Sunday'); 
    $weekDays = []; 

    for ($i = 0; $i < $NUMBER_OF_COLUMNS; $i++) { 
     $weekDays[] = strftime('%a', $timestamp); 
     $timestamp = strtotime('+1 day', $timestamp); 
    } 

    $blank = date('w', strtotime("{$year}-{$month}-01")); 
    $divid = $title . $year; 

    return view('transaction.index', [ 
     'date' => $date, 
     'day' => $day, 
     'month' => $month, 
     'year' => $year, 
     'firstDay' => $firstDay, 
     'title' => $title, 
     'dayOfWeek' => $dayOfWeek, 
     'daysInMonth' => $daysInMonth, 
     'timestamp' => $timestamp, 
     'weekDays' => $weekDays, 
     'blank' => $blank, 
     'divid' => $divid, 
     'NUMBER_OF_COLUMNS' => $NUMBER_OF_COLUMNS 
    ]); 
} 

我只得到這一點。謝謝您的幫助!

enter image description here

+1

問題是你'返回'_side_每次迭代。這意味着您在_first_ iteration期間跳出該函數(返回調用範圍),以便後續迭代不會執行。 _不返回!_如果緩衝輸出,則將其連接到某個字符串值並最終返回。 – arkascha

+0

還有一個一般提示:以這種方式交換代碼是非常痛苦的。我建議你把你的代碼放入一些修改控制系統,比如git或subversion版本庫(github就好)。這樣,其他人可以使用您的代碼並創建您可以查看和接受或拒絕的修改。這就是代碼開發如何爲您提高效率。 – arkascha

+0

我該怎麼做? 「如果你緩衝輸出,然後連接它到一些字符串值並最終返回。」 @arkascha –