2014-12-02 114 views
-1

我需要的是下個月的日子,新的一天上線。 其實我希望它看起來未來:php下個月的日期

date  | username1 | username2 
_________|___________|__________ 
     |   | 
Mon - 01 | user5  | user2 
     |   | 
Tue - 02 | user3  | user2 

.... 
and so on 

以後我會收集這些日期和用戶名並更新SQL表(同一類型的,像這裏的例子)。

我有這樣的代碼:

$workdays = array(); 
$type = CAL_GREGORIAN; 
$month = date('n'); // Month ID, 1 through to 12. 
$year = date('Y'); // Year in 4 digit 2009 format. 
$day_count = cal_days_in_month($type, $month, $year); // Get the amount of days 
//loop through all days 
for ($i = 1; $i <= $day_count; $i++) { 
$date = $year.'-'.$month.'-'.$i; //format date 
$workdays[] = $i; 

,並用foreach回聲我得到天1,2,3 ...等正確,大家對新的生產線。但是,如果我把裏面的foreach這樣的:

<?php foreach($workdays as $value['date']): ?> 
<tr> 
<td><?php if ($value['date'] != 'dd'){ 
    setlocale(LC_TIME, 'fi_FI.UTF-8'); 
    echo ucfirst(strftime("%a - %d", strtotime($value['date']))); 
    } else { 
    echo 'wrong'; 
    }?></td> 
<td>&nbsp;</td> 
</tr> 
<?php endforeach ?> 

我得到的每一個新行週一 - 01

什麼是錯的(在那之後,我新受在PHP)?

+2

您的完整代碼在哪裏?我不能在'​​'周圍看到任何'foreach'。你初始化'$ value'的地方? – 2014-12-02 20:26:00

+0

如果我們猜測'$ value ['date']'是取出的行...那麼你需要通過'mysql_fetch_array()'獲取所有行,我認爲你做了一次。所以每次你得到星期一 - 01! – Riad 2014-12-02 20:28:22

+0

有一刻,我現在編輯帖子。 – avtech 2014-12-02 20:28:45

回答

0

嘗試類似這樣的事情。

<?php 

$day_count = date('t',strtotime('+1 month')); 
$month = date('m'); 
$year = date('Y'); 

if ($month == 12) { $month = 1; $year++; } 

for($i = 1; $i<=$day_count; $i++) { 
    echo '<tr>'; 
    echo '<td>'.date('D',strtotime("$month/$i/$year")).' '.$i."</td>"; 
    echo '<td>&nbsp;</td>'; 
    echo '</tr>'; 
} 
+0

非常感謝!這一個像我需要的工作。我再次學到了很多東西。 – avtech 2014-12-03 07:00:25