PHP:每隔X周进行日历,重复活动

时间:2011-09-30 17:40:59

标签: php calendar php-5.3

我试图从给定日期开始每隔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是一小时,我不知道这是从哪里来的。奇怪的是,快进到2012年3月26日它再次开始工作但同样的错误发生在2012年10月29日......

我已经在这里待了几个小时而且无法理解这一点。任何帮助真的很感激!

由于

2 个答案:

答案 0 :(得分:0)

在某些国家,有一个夏天&amp;时钟转换一小时的冬季时间(@ 27 march&amp; 30 oc​​t)。因此有一个小时的差异。

- &GT; 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;
    }
}

?>

答案 1 :(得分: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;
}