带夏令时的PHP日期时间

带夏令时的PHP日期时间,php,datetime,Php,Datetime,因此,我正在构建一个网站应用程序,用于计算任何用户的账单到期金额。在下个月账单到期之前,我设法使所有的计算工作顺利进行。计算用户在账单到期前收到的工资支票数量时出现了问题。在几次var_转储之后,我意识到当我将天数添加到循环中时,我得到了额外的3600秒(3月12日夏令时获得的1小时)。这里有一个峰值,在代码中计算所有的东西。只是为了那些在这篇文章发布几个月后看到这篇文章的人。今天的当前日期是2017年2月27日 //declare paychecks as counter $paych

因此,我正在构建一个网站应用程序,用于计算任何用户的账单到期金额。在下个月账单到期之前,我设法使所有的计算工作顺利进行。计算用户在账单到期前收到的工资支票数量时出现了问题。在几次var_转储之后,我意识到当我将天数添加到循环中时,我得到了额外的3600秒(3月12日夏令时获得的1小时)。这里有一个峰值,在代码中计算所有的东西。只是为了那些在这篇文章发布几个月后看到这篇文章的人。今天的当前日期是2017年2月27日

  //declare paychecks as counter
  $paychecks = 0;

  //the number of days a user has between paychecks
  $frequency = 14; 

  //users next payday
  $next_payday = strtotime(2017-03-10);

  //the date the next bill is due
  $due_date = strtotime(2017-03-24);
理论上,在到期日之前应该有两张工资单。(第二次发薪发生在账单到期日)

while($next_payday add(新的日期间隔('P'.$frequency.'D'));
$next_payday+=strotime($date_array->format('Y-m-d');
//我把它注释掉了,但这也不起作用
//$next_payday+=($frequency*86400);
//增加计数器
$paychecks++;
}
因此在理论上(除了DST是一个因素外,这在任何其他时间都有效)我试图确定用户在账单到期之前有多少张支票。问题是此实例返回1而不是2,因为在循环的第二次迭代发生时,$next\u payday实际上会额外增加3600秒。这使得$next\u payday比$due\u dates值高3600秒。我假设是因为D圣

那么我是否应该比较字符串值(date('Y-m-d',$due\u date)=date('Y-m-d',$next\u payday))相反,当到期日与下一个发薪日相同时,这将起作用,但当到期日大于或小于时,这将不起作用。我注意到,当将这些日期转换回字符串格式时,它们是相同的。或者有没有更好的方法来实现这一点,我没有找到

在下一个发薪日增加3600美元的同时,它也会通过while循环工作,但我真的不想这样做。我确信,当DST再次发生时,它会把我搞得一团糟,我会损失一个小时


感谢您的输入。

使用
86400
(60*60*24)
增加时间将在遇到DST事件时扭曲您的结果。幸运的是
strotime()
不会受到添加天数、周数等的影响

DateTime构造很好,但我还没有在任何简单的DateTime进程中使用它。这种情况也不例外

当我测试代码时,它从未进入while循环,因为
strotime()
值没有被引用,因此被转换成意外的时间戳,设置
$next\u payday
大于
$due\u date

此代码将正确计算日期范围内的付款期数:

//declare paychecks as counter
$paychecks = 0;

//the number of days a user has between paychecks
$frequency = 14;
// or you could use 2 and set the strtotime unit to "+$frequency weeks"

//users next payday
$next_payday = strtotime("2017-03-10");  // this date value needed quotes

//the date the next bill is due
$due_date = strtotime("2017-03-24");  // this date value needed quotes

//echo date("Y-m-d",$next_payday),"<br>",date("Y-m-d",$due_date),"<br>";

while($next_payday<=$due_date){
    ++$paychecks;  // 2017-03-10 & 2017-03-24
    $next_payday=strtotime(date("Y-m-d",$next_payday)." +$frequency days");
}

//echo "next_payday=",date("Y-m-d",$next_payday),"<br>"; // this will be beyond $due_date
echo $paychecks;  // 2
//将工资支票声明为计数器
$paychecks=0;
//用户在工资支票之间的天数
$frequency=14;
//或者您可以使用2并将strotime单位设置为“+$frequency weeks”
//用户下一个发薪日
$next_payday=strotime(“2017-03-10”);//此日期值需要报价
//下一张账单到期的日期
$due_date=strotime(“2017-03-24”);//此日期值需要报价
//回音日期(“Y-m-d”,“下一个发薪日”),“
”,日期(“Y-m-d”,“到期日”),“
”;
既然($next_payday)为什么要把strotime()和DateTime混在一起呢?因为首先我在它上面加了86400秒。这部分被注释掉了
//declare paychecks as counter
$paychecks = 0;

//the number of days a user has between paychecks
$frequency = 14;
// or you could use 2 and set the strtotime unit to "+$frequency weeks"

//users next payday
$next_payday = strtotime("2017-03-10");  // this date value needed quotes

//the date the next bill is due
$due_date = strtotime("2017-03-24");  // this date value needed quotes

//echo date("Y-m-d",$next_payday),"<br>",date("Y-m-d",$due_date),"<br>";

while($next_payday<=$due_date){
    ++$paychecks;  // 2017-03-10 & 2017-03-24
    $next_payday=strtotime(date("Y-m-d",$next_payday)." +$frequency days");
}

//echo "next_payday=",date("Y-m-d",$next_payday),"<br>"; // this will be beyond $due_date
echo $paychecks;  // 2
while($next_payday<=$due_date && $next_payday=strtotime(date("Y-m-d",$next_payday)." +$frequency days")){++$paychecks;}