[英]How to create recurring event with every 2 days/weeks/months/years in Google Calendar API PHP
[英]PHP: Every X weeks for Calendar, Recurring Events
我正在尝试从给定日期起每隔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日发生了同样的错误...
我已经在这里呆了几个小时了,无法解决这个问题。 任何帮助真的很感激!
谢谢
在某些情况下,夏令时和冬令时将时钟转换为一小时(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;
}
}
?>
我已经给您答案,以回答问题和努力。 最后,我重新考虑并做了更多的谷歌搜索,这就是我想出的。
$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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.