PHP:日历中每 X 周一次,重复事件

发布于 2024-12-08 01:24:10 字数 1723 浏览 0 评论 0原文

我正在尝试从日历的给定日期开始每隔 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 日又出现同样的错误......

我已经在这个问题上呆了几个小时了,但无法弄清楚这一点。任何帮助真的很感激!

谢谢

I'm trying to get every X days from a given date for a calendar. I think I have the logic down to an extent, but for a small window of dates I am getting unexpected results.

My code below taken from my project:

$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)));                                               
}

Outputs (current date - start date - modular) :

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

The last few lines are the issue for me.

3600 is one hour and I don't know where this is coming from. Strange thing is, fast forward to march 26 2012 and it starts to work again but same error occurs at Oct 29 2012...

I've been at this for a few hours and can't figure this out. Any help really appreciated!

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

栀梦 2024-12-15 01:24:10

在某些国家/地区有夏季和夏季。冬季时间,时钟转换一小时(@ 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;
    }
}

?>

In some contries there is a summer & winter time where the clock is converted one hour (@ 27 march & 30 oct). Therefore there is a one hour difference.

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

Example:

<?

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

?>
青丝拂面 2024-12-15 01:24:10

我已经给了你回答问题的答案和努力。最后,我重新思考并进行了更多谷歌搜索,这就是我想出的结果。

$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;
}

I have given you the answer for answering the question and the effort. In the end, I had a rethink and did more googling and this is what I have come up with.

$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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文