2011-03-21 34 views
2

下面開始是我已經爲越來越星期日作爲一週的開始日創建的函數,問題與得到周從週日


function getCurrentIntervalOfWeek($liveratetime) { 
    // get start of each week. 
    $dayofweek = date('w', $liveratetime); 
    $getdate = date('Y-m-d', $liveratetime); 
    $createstart = strtotime('last Sunday', $getdate); 
    $weekstart = ($dayofweek == 0) ? $liveratetime : $createstart; 
    // get the current time interval for a week, i.e. Sunday 00:00:00 UTC 
    $currentInterval = mktime(0,0,0, date('m', $weekstart), date('d', $weekstart), date('Y', $weekstart)); 
    return $currentInterval; 
} 

這裏liveratetime是任何一天在一個星期的出現時間。基本上這個函數需要活動時間並尋找最後一個星期天,以便獲得該活動時間段的當前時間間隔。

但這裏的問題是,無論何時我試圖從某個活動時間獲得當前時間間隔

$createstart = strtotime('last Sunday', $getdate);
給我
-345600
。我不明白爲什麼?任何人都可以請分享一下這個。

這通常發生在過去的日期一樣

2007-10-02

回答

1

的strtotime的第二個參數是一個時間戳,而不是日期的字符串表示。 嘗試:

$createstart = strtotime('last Sunday', $liveratetime); 

它給你-345600,因爲當$ GETDATE,這是Y-M-d,被解析爲0一個int結果 - 劃時代的時間。因此,從時代最後一個星期天開始的結果是...

+1

@ srahul07正確閱讀Bredis的回答......他正在談論你對'strtotime'的函數調用。 – Jacob 2011-03-21 11:15:16

+0

@Jacob。 Yaa ...你是對的。收拾我的錯誤。謝謝。 – srahul07 2011-03-21 11:21:58

2

您可能想要嘗試此功能,它會根據日期提供的日期從週日開始返回一個日期數組。

function get_week_dates($date) 
{ 
// the return array 
$dates = array(); 

$time = strtotime($date); 
$start = strtotime('last Sunday', $time); 

$dates[] = date('Y-m-d', $start); 

// calculate the rest of the times 
for($i = 1; $i < 7; $i++) 
{ 
    $dates[] = date('Y-m-d' , ($start + ($i * (60 * 60 * 24)))); 
} 

return $dates; 
} 

使用

get_week_dates('2011-03-21'); 

將返回

array 
    0 => string '2011-03-20' (length=10) 
    1 => string '2011-03-21' (length=10) 
    2 => string '2011-03-22' (length=10) 
    3 => string '2011-03-23' (length=10) 
    4 => string '2011-03-24' (length=10) 
    5 => string '2011-03-25' (length=10) 
    6 => string '2011-03-26' (length=10) 
2

我一直在尋找這個問題和一些經過研究解決方案,並試圖,這似乎是這個工程......雖然我仍然需要測試是在下週日看它是否真的..無論如何這裏是代碼:

$week_start = new DateTime(); 
$week = strftime("%U"); //this gets you the week number starting Sunday 
$week_start->setISODate(2012,$week,0); //return the first day of the week with offset 0 
echo $week_start -> format('d-M-Y'); //and just prints with formatting