2011-09-30 75 views
0

我想每X天的時間從日曆中給定的日期。我認爲我的邏輯在一定程度上有所下降,但是對於一小段日期我收到了意想不到的結果。PHP:每X週日歷,重複活動

我的代碼從我的項目採取如下:

$start_time = 1345935600; //Sept 25th 2011; 
$end_time = 1355011200; //Dec 9th 2011; 

$sStartDate = date("Y-m-d", $start_time) . " " . date("H:i:s", strtotime("00:00:00")); 
$sEndDate = date("Y-m-d", $end_time) . " " . date("H:i:s", strtotime("00:00:00")); 
$sCurrentDate = $sStartDate; 

while($sCurrentDate < $sEndDate) 
{          
    $t_date = strtotime($sCurrentDate); 
    $s_date = strtotime(date("Y-m-d", strtotime("08/30/2011"))); 

    $recurs_every = 60 * 60 * 24 * 2; //occurs every 2 days 

    echo date("Y-m-d H:i:s", $t_date) . " " . date("Y-m-d H:i:s", $s_date) . " " . (($t_date - $s_date) % $recurs_every) . " " . $recurs_every . "<BR>" ; 

    $sCurrentDate = date("Y-m-d", strtotime("+1 day", strtotime($sCurrentDate)));            
} 

輸出(當前日期 - 開始日期 - 模塊化):

2011-09-25 00:00:00 2011-08-30 00:00:00 0 
2011-09-26 00:00:00 2011-08-30 00:00:00 86400 
.... 
2012-10-27 00:00:00 2011-08-30 00:00:00 0 
2012-10-28 00:00:00 2011-08-30 00:00:00 86400 
2012-10-29 00:00:00 2011-08-30 00:00:00 3600 
2012-10-30 00:00:00 2011-08-30 00:00:00 90000 
2012-10-31 00:00:00 2011-08-30 00:00:00 3600 
2012-11-01 00:00:00 2011-08-30 00:00:00 90000 
2012-11-02 00:00:00 2011-08-30 00:00:00 3600 
2012-11-03 00:00:00 2011-08-30 00:00:00 90000 
2012-11-04 00:00:00 2011-08-30 00:00:00 3600 
2012-11-05 00:00:00 2011-08-30 00:00:00 90000 

的最後幾行是我的問題。

3600是一個小時,我不知道這是哪裏來的。奇怪的是,快進至3月26日2012年它再次開始工作,但在2012年10月29日...

我已經在這幾個小時,不能算出這個發生同樣的錯誤。任何幫助真的很感激!

由於

回答

0

在一些contries有一個夏天&冬季時間在時鐘被轉換一個小時(@ 3月27日& 10月30日)。因此有一個小時的差異。

- >http://en.wikipedia.org/wiki/Daylight_saving_time

例子:

<? 

error_reporting(E_ALL); 

$start_time = 1345935600; //Sept 25th 2011; 
$end_time = 1355011200; //Dec 9th 2011; 

$sStartDate = date("Y-m-d", $start_time) . " " . date("H:i:s", strtotime("00:00:00")); 
$sEndDate = date("Y-m-d", $end_time) . " " . date("H:i:s", strtotime("00:00:00")); 
$sCurrentDate = $sStartDate; 

$lastDate = 0; 
$corr = 0; 

while($sCurrentDate < $sEndDate) 
{          
    $t_date = strtotime($sCurrentDate); 
    $s_date = strtotime(date("Y-m-d", strtotime("08/30/2011"))); 

    $recurs_every = 60 * 60 * 24 * 2; //occurs every 2 days 

    echo date("Y-m-d H:i:s", $t_date) . " " . date("Y-m-d H:i:s", $s_date) . " " . (($t_date - $s_date + $corr) % $recurs_every) . " " . $recurs_every . "<BR>" ; 

    $lastDate = strtotime($sCurrentDate); 
    $sCurrentDate = date("Y-m-d", strtotime("+1 day", strtotime($sCurrentDate)));    

    if(strtotime($sCurrentDate) - $lastDate != 60 * 60 * 24) { 
     $corr -= strtotime($sCurrentDate) - $lastDate - 60 * 60 * 24; 
    } 
} 

?> 
+0

我認爲這可能與此有關。我的大腦目前正在發展。你能否更新我的代碼來解決它? – PaulM

+0

我編輯了我的帖子。應該是這樣的。可能不是最好的解決方案,但應該工作(在我的測試中)。 – MasterCassim

0

我已經給了你答案回答問題和努力。最後,我重新思考並做了更多的搜索,這就是我所想到的。

$start_time = 1316905200; 
$end_time = 1320537600; 

$sStartDate = date("Y-m-d", $start_time); 
$sEndDate = date("Y-m-d", $end_time); 

$sStartDate = new DateTime($sStartDate); 
$sEndDate = new DateTime($sEndDate); 

$sCurrentDate = $sStartDate; 
$recurs_every = 60 * 60 * 24 * 2; //occurs every 2 days 

while($sCurrentDate->format('U') < $sEndDate->format('U')) 
{  
    $s_date = new DateTime("08/30/2011"); 

    echo $sCurrentDate->format('Y-m-d') . " " . $s_date->format('Y-m-d') . " " . ceil(($sCurrentDate->format('U') - $s_date->format('U') - (chkTransitions($sCurrentDate->format('U')) == 'GMT' ? 3600 : 0)) % $recurs_every) . "<BR>"; 

    $sCurrentDate->add(new DateInterval('P1D'));  
} 

function chkTransitions($dt) 
{ 
    $timezone = new DateTimeZone("Europe/London"); 
    $transitions = $timezone->getTransitions($dt, $dt); 

    $offset = $transitions[0]['offset']; 
    $abbr = $transitions[0]['abbr']; 

    return $abbr; 
}