PHP:日历、重复事件每X周一次
我正在尝试从给定日期开始每隔X天获取一个日历。我想我在某种程度上已经有了逻辑,但对于一小段日期,我得到了意想不到的结果 以下代码取自我的项目:PHP:日历、重复事件每X周一次,php,calendar,php-5.3,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")); $sEnd
$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)));
}
最后几行是我的问题
3600是一个小时,我不知道这是从哪里来的。奇怪的是,快进到2012年3月26日,它又开始工作了,但同样的错误发生在2012年10月29日
这件事我已经做了几个小时了,还没弄明白。非常感谢您的帮助
谢谢在一些国家,在夏季和冬季,时钟在3月27日和10月30日转换为1小时。因此有1小时的时差 -> 例如:
<?
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;
}
我想这可能与此有关。我现在脑子乱糟糟的。你可以更新我的代码来解决这个问题吗?我编辑了我的帖子。应该是这样的。可能不是最好的解决方案,但在我的测试中应该是可行的。
$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;
}