2011-04-27 44 views
0

28天爲四周正確的?那麼,我試圖寫一個函數,基本上返回像這樣的一週,第一週,第二週,第三週,第四周..Week1實質上是從今天開始的前7天。但是,就我所能去的而言。使用phptime函數輸出星期數的函數

function week() { 
    $currentdate = time(); 
    $numberofdays = 28; 
    for ($i=0; $i<$numberofdays; $i++) { 
    } 
} 
+0

if(i%7 == 0)echo「week:」。$ j ++; – 2011-04-27 21:25:52

+0

@Byron Whitlock,這不使用當前日期? – seun 2011-04-27 21:31:07

+0

請顯示您期望的日期的輸出。 – 2011-04-27 21:56:43

回答

1

那麼,你試圖保持所有的日子,如日曆,或簡單地生成每週開始的那一天?

如果你試圖讓所有的日子,試試這個:

function week($days = 28) 
{ 
    //Note, I added the number of days to the function arguments, so that it can be variable without having to change the code 
    if(!is_int($days) || $days <= 0) 
    { 
     return false; 
    } 

    $start = strtotime("midnight tonight"); 
    $currentweek = 1; 
    $weeks = array(); 
    for ($i = 1; $i <= $days; $i++) 
    { 
     $weeks[$currentweek][] = $start + ($i * 86400); 

     if(!(i % 7)) 
     { 
      $currentweek++; 
     } 
    } 

    return $weeks; 
} 

這應返回時間戳的陣列,由周分組,從中可以運行$days號當天午夜開始的日子。如果您想要格式正確的日期,而不是將時間戳存儲在數組中,請將date()函數的結果存儲在時間戳中。

0

看到文檔的位置:

http://www.php.net/manual/en/function.time.php

我想你指的是這樣的:

<?php 
$nextWeek = time() + (7 * 24 * 60 * 60); 
        // 7 days; 24 hours; 60 mins; 60secs 
echo 'Now:  '. date('Y-m-d') ."\n"; 
echo 'Next Week: '. date('Y-m-d', $nextWeek) ."\n"; 
// or using strtotime(): 
echo 'Next Week: '. date('Y-m-d', strtotime('+1 week')) ."\n"; 
?> 

上面的例子將輸出的東西類似:

現在:2005-03-30下週: 2005-04-06下週:2005-04-06

+0

如果恰好在DST切換時間附近,則會中斷。和往常一樣,在日期計算中使用時間戳。 – AndreKR 2011-04-27 22:59:04

0
/** 
* Returns the amount of weeks into the month a date is 
* @param $date a YYYY-MM-DD formatted date 
* @param $rollover The day on which the week rolls over 
*/ 
function getWeeks($date, $rollover) 
{ 
    $cut = substr($date, 0, 8); 
    $daylen = 86400; 

    $timestamp = strtotime($date); 
    $first = strtotime($cut . "00"); 
    $elapsed = ($timestamp - $first)/$daylen; 

    $i = 1; 
    $weeks = 1; 

    for($i; $i<=$elapsed; $i++) 
    { 
     $dayfind = $cut . (strlen($i) < 2 ? '0' . $i : $i); 
     $daytimestamp = strtotime($dayfind); 

     $day = strtolower(date("l", $daytimestamp)); 

     if($day == strtolower($rollover)) $weeks ++; 
    } 

    return $weeks; 
} 


$dateNow = date("Y-m-d"); 
echo getWeeks($dateNow, "monday");