PHP 计算重复事件的未来日期 - 只能工作到 2037 年?
我正在编写一个基于日历的 Web 应用程序,并且正在编写一些代码,这些代码将循环 X 次并计算出开始/结束日期和日期。未来事件的时间。每个事件都有一个开始日期/时间和一个结束日期/时间。循环运行得非常好,直到 2037 年,此时循环停止功能正常。转换从未发生,并将我的日期恢复到 1969 年。请参阅下面的代码和输出。有人能告诉我为什么会发生这种情况吗?
问题行为的实时示例:http://codepad.org/h5ET3h2O
$date1 = strtotime('August 17 2011 8:00 AM');
for ($x=1; $x<=$repeatTimes; $x++){
echo date('m/d/Y g:i A', $date1)."<br><br>".$date1 ." +". $repeatFreq." ".$repeatUnit. " = ";
$date1 = strtotime(date('m/d/Y g:i A', $date1) . " +".$repeatFreq." ".$repeatUnit );
echo $date1."<br /><br />";
}
输出
08/15/2011 8:00 AM
1313409600 +1 Year = 1345032000
08/15/2012 8:00 AM
1345032000 +1 Year = 1376568000
08/15/2013 8:00 AM
1376568000 +1 Year = 1408104000
//Some Left Out
08/15/2036 8:00 AM
2102414400 +1 Year = 2133950400
//Problem starts here
08/15/2037 8:00 AM
2133950400 +1 Year =
12/31/1969 7:00 PM
+1 Year = 31536000
12/31/1970 7:00 PM
31536000 +1 Year = 63072000
12/31/1971 7:00 PM
63072000 +1 Year = 94694400
12/31/1972 7:00 PM
94694400 +1 Year = 126230400
12/31/1973 7:00 PM
126230400 +1 Year = 157766400
12/31/1974 7:00 PM
157766400 +1 Year = 189302400
//Some Left Out
更新
我能够利用 DateTime 类来使日期正常工作。
for ($x=1; $x<=$repeatTimes; $x++){
$date1->modify(" +".$repeatFreq." ".$repeatUnit);
$sDates[]=$date1->format('m/d/Y g:i A');
}
I am writing a calendar-based web application and am working on some code that will loop X amount of times and figure out the start/end dates & times of the future events. Each event has a start date/time and an end date/time The loop runs perfectly fine up until the year 2037, at which point the loop stop function properly. The conversion never takes place and reverts my dates back to 1969. Please see the code and output below. Can someone tell me why this is happening?
Live Example of Problem Behavior: http://codepad.org/h5ET3h2O
$date1 = strtotime('August 17 2011 8:00 AM');
for ($x=1; $x<=$repeatTimes; $x++){
echo date('m/d/Y g:i A', $date1)."<br><br>".$date1 ." +". $repeatFreq." ".$repeatUnit. " = ";
$date1 = strtotime(date('m/d/Y g:i A', $date1) . " +".$repeatFreq." ".$repeatUnit );
echo $date1."<br /><br />";
}
Output
08/15/2011 8:00 AM
1313409600 +1 Year = 1345032000
08/15/2012 8:00 AM
1345032000 +1 Year = 1376568000
08/15/2013 8:00 AM
1376568000 +1 Year = 1408104000
//Some Left Out
08/15/2036 8:00 AM
2102414400 +1 Year = 2133950400
//Problem starts here
08/15/2037 8:00 AM
2133950400 +1 Year =
12/31/1969 7:00 PM
+1 Year = 31536000
12/31/1970 7:00 PM
31536000 +1 Year = 63072000
12/31/1971 7:00 PM
63072000 +1 Year = 94694400
12/31/1972 7:00 PM
94694400 +1 Year = 126230400
12/31/1973 7:00 PM
126230400 +1 Year = 157766400
12/31/1974 7:00 PM
157766400 +1 Year = 189302400
//Some Left Out
UPDATE
I was able to utilize the DateTime Class to get the date working properly.
for ($x=1; $x<=$repeatTimes; $x++){
$date1->modify(" +".$repeatFreq." ".$repeatUnit);
$sDates[]=$date1->format('m/d/Y g:i A');
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
http://en.wikipedia.org/wiki/Unix_time
您已达到最大值32 位整数。
时间以自 1970 年 1 月 1 日(UTC)以来的秒数来衡量。您看到 1969 年是因为在您的时区,这就是发生这种情况的时间。当秒数溢出 32 位 int 时,它会循环回来。
您可以通过以下方式确认最大整数值:
echo PHP_INT_MAX;
。如果您想要 64 位整数,则需要使用 64 位硬件和 64 位版本的 PHP(现在通常需要 自己编译,因为我不相信 PHP.net 上有官方二进制文件)。
还有其他选择。请参阅此处:如何在 PHP 上使用 64 位整数?
编辑:根据 Maerlyn 的建议,您应该考虑使用 DateTime PHP 最新版本中内置的类。只要您坚持使用它,它应该可以帮助您解决这个问题。
http://en.wikipedia.org/wiki/Unix_time
You're hitting the max value of a 32-bit integer.
Time is measured as the number of seconds since January 1, 1970 UTC. You see 1969 because in your timezone, that's when this would occur. When the number of seconds overflows the 32-bit int, it loops back around.
You can confirm the max integer value with this:
echo PHP_INT_MAX;
.If you want 64-bit integers, you need to use 64-bit hardware, and a 64-bit version of PHP (which usually these days, you have to compile yourself, as I don't believe official binaries are available on PHP.net yet).
There are also other options. See here: how to have 64 bit integer on PHP?
Edit: Per Maerlyn's suggestion, you should consider using the DateTime class built into recent versions of PHP. It should get you around this problem, as long as you use it consistently.