PHP:日历中每 X 周一次,重复事件
我正在尝试从日历的给定日期开始每隔 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在某些国家/地区有夏季和夏季。冬季时间,时钟转换一小时(@ 3 月 27 日和 10 月 30 日)。因此有一个小时的时差。
-> http://en.wikipedia.org/wiki/Daylight_ saving_time
示例:
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:
我已经给了你回答问题的答案和努力。最后,我重新思考并进行了更多谷歌搜索,这就是我想出的结果。
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.