2011-06-15 34 views

回答

3

如果你使用PHP 5.2 +,你可以使用我寫的庫,以便處理日期遞歸在PHP稱爲When

與庫,代碼將是這樣的:

$r = new When(); 
$r->recur(<start date here>, 'weekly') 
    ->until(<end date here>) 
    ->wkst('SU') 
    ->byday(array('MO', 'TU', 'WE', 'TH', 'FR')); 

while($result = $r->next()) 
{ 
    echo $result->format('c') . '<br />'; 
} 
+0

你先生,是男人! :)在1去工作!謝謝! – Sander 2011-06-15 15:14:00

1

我想你可以遍歷日期並檢查每一個日期,並增加一個計數器。

想不到頭頂上的其他東西。

+0

這當然可以工作。 – jefflunt 2011-06-15 14:37:54

1

僞未來的路:

現在計算之間的天數,月 的最後一天獲得了一週的當天(即星期三) 基礎上一週的當天,和在該月剩餘的天數中,計算該月剩餘多少個週末天數是簡單的計算 - 這將是該月剩餘的天數,減去該月剩餘的週日/週六的數量。

我會寫一個函數,是這樣的:

daysLeftInMonth(daysLeftInMonth, startingDayOfWeek, dayOfWeekToCalculate) 

其中:

  • daysLeftInMonth是一個月(30)的最後一天,減去當前的日期(15)
  • startingDayOfWeek是你想要開始的一週中的哪一天(今天是星期三)
  • dayOfWeekToCalculate是你想要計算的星期幾,例如週六或週日。 2011年6月目前擁有2日及2個星期六離開,直到本月

因此結束,你的算法變成類似:

getWeekdaysLeft(todaysDate) 

... getWeekdaysLeft是一樣的東西:

sundaysLeft = daysLeftInMonth(lastDayOfMonth - todaysDate, "Wednesday", "Sunday"); 
saturdaysLeft = daysLeftInMonth(lastDayOfMonth - todaysDate, "Wednesday", "Saturday"); 
return ((lastDayOfMonth - todaysDate) - (sundaysLeft + saturdaysLeft)); 
1

該代碼至少執行了一部分要求。而不是「下個月底」,它只是在給定的天數內工作。

$dfrom = time(); 
$fourweeks = 7 * 4; 

for ($i = 0; $i < $fourweeks; $i ++) { 
    $stamp = $dfrom + ($i * 24 * 60 * 60); 
    $weekday = date("D", $stamp); 
    if (in_array($weekday, array("Mon", "Tue", "Wed", "Thu", "Fri"))) { 
     print date(DATE_RSS, $stamp) . "\n"; 
    } 
} 
2

這裏試試:http://codepad.org/wuAyAqnF

要使用它,只需一個時間戳傳遞給get_weekdays。在當月的其餘時間,您將獲得所有工作日的數組,作爲時間戳。或者,您可以通過$to參數 - 您將獲得$from$to之間的所有工作日。

function get_weekdays ($from, $to=false) { 
    if ($to == false) 
     $to = last_day_of_month($from); 

    $days = array(); 

    for ($x = $from; $x < $to; $x+=86400) { 
     if (date('w', $x) > 0 && date('w', $x) < 6) 
      $days[] = $x; 
    } 
    return $days;  
} 

function last_day_of_month($ts=false) { 
    $m = date('m', $ts); 
    $y = date('y', $ts); 
    return mktime(23, 59, 59, ($m+1), 0, $y); 
} 
2

本示例以快速而有效的方式正好滿足您的需求。 它不會做嵌套循環,並使用完全真棒DateTime對象。

$oDateTime = new DateTime(); 
$oDayIncrease = new DateInterval("P1D"); 

$aWeekDays = array(); 
$sStart = $oDateTime->format("m-Y"); 
while($oDateTime->format("m-Y") == $sStart) { 
    $iDayInWeek = $oDateTime->format("w"); 
    if ($iDayInWeek > 0 && $iDayInWeek < 6) { 
     $aWeekDays[] = clone $oDateTime; 

    } 
    $oDateTime->add($oDayIncrease); 
} 
1
// Find today's day of the month (i.e. 15) 
$today = intval(date('d')); 

// Define the array that will hold the work days. 
$work_days = array() 

// Find this month's last day. (i.e. 30) 
$last = intval(date('d', strtotime('last day of this month'))); 

// Loop through all of the days between today and the last day of the month (i.e. 15 through 30) 
for ($i = $today; $i <= $last; $i++) 
{ 
    // Create a timestamp. 
    $timestamp = mktime(null, null, null, null, $i); 

    // If the day of the week is greater than Sunday (0) but less than Saturday (6), add the timestamp to an array. 
    if (intval(date('w', $timestamp)) > 0 && intval(date('w', $timestamp)) < 6) 
    $work_days[] = mktime($timestamp); 
} 

$work_days陣列將包含你可以用這種方式時間戳:上面的代碼與工作

echo date('Y-m-d', $work_days[0]); 

在PHP 4以及PHP 5中。它不依賴於DateTime類的功能,該功能在PHP 5.2之前是不可用的,並且不需要使用由其他人。

相關問題