PHP 除法导致 NaN - 可能与 DateTime 函数有关
我目前正在尝试执行一个“很久以前”的函数,但由于某种原因,它最终给了我一个 NaN 的结果。
我正在使用这段代码的稍微修改过的版本(位于: http://www.sitepoint.com/create-your-own-twitter-widget-3/ )
if ($this->DateFormat == 'friendly') {
// friendly date format
$ival = $now->diff($stime);
$yr = $ival->y;
$mh = $ival->m + ($ival->d > 15);
if ($mh > 11) $yr = 1;
$dy = $ival->d + ($ival->h > 15);
$hr = $ival->h;
$mn = $ival->i + ($ival->s > 29);
if ($yr > 0) {
if ($yr == 1) $date = 'last year';
else $date = $yr . ' years ago';
}
else if ($mh > 0) {
if ($mh == 1) $date = 'last month';
else $date = $mh . ' months ago';
}
else if ($dy > 0) {
if ($dy == 1) $date = 'yesterday';
else if ($dy < 8) $date = $dy . ' days ago';
else if ($dy < 15) $date = 'last week';
else $date = round($dy / 7) . ' weeks ago';
}
else if ($hr > 0) {
$hr += ($ival->i > 29);
$date = $hr . ' hour' . ($hr == 1 ? '' : 's') . ' ago';
}
else {
if ($mn < 3) $date = 'just now';
else $date = $mn . ' minutes ago';
}
}
(有问题的行是带有 round($dy / 7)
我创建了一组可能的值来测试结果,它给我一个 NaN 表示 -29 天的日期,问题是,只有当我一遍又一遍地重复该函数时,如果我只测试 -29 天,它就会输出正确的 4 周值
。我该如何避免/修复这个问题?
编辑
这很可能是 PHP 中的一个错误,
如果我让代码进行两次除法和舍入,NaN 永远不会出现,
// do estimate of how many weeks ago
$round = round($days / 7, 0);
$round = round($days / 7, 0);
$date = $round . ' weeks ago';
因为在我的代码中 。测试循环,触发周计算的第一个值(假设我正在进行多次计算)会给出 NaN。
I'm currently trying to do a "time ago" function but for some reason, it ends up giving me a NaN for one of the results.
I'm using a slightly modified version of this piece of code (found at: http://www.sitepoint.com/create-your-own-twitter-widget-3/ )
if ($this->DateFormat == 'friendly') {
// friendly date format
$ival = $now->diff($stime);
$yr = $ival->y;
$mh = $ival->m + ($ival->d > 15);
if ($mh > 11) $yr = 1;
$dy = $ival->d + ($ival->h > 15);
$hr = $ival->h;
$mn = $ival->i + ($ival->s > 29);
if ($yr > 0) {
if ($yr == 1) $date = 'last year';
else $date = $yr . ' years ago';
}
else if ($mh > 0) {
if ($mh == 1) $date = 'last month';
else $date = $mh . ' months ago';
}
else if ($dy > 0) {
if ($dy == 1) $date = 'yesterday';
else if ($dy < 8) $date = $dy . ' days ago';
else if ($dy < 15) $date = 'last week';
else $date = round($dy / 7) . ' weeks ago';
}
else if ($hr > 0) {
$hr += ($ival->i > 29);
$date = $hr . ' hour' . ($hr == 1 ? '' : 's') . ' ago';
}
else {
if ($mn < 3) $date = 'just now';
else $date = $mn . ' minutes ago';
}
}
(the problematic line is the one with the round($dy / 7)
I created a set of possible values to test the outcomes and it gives me a NaN for a date of -29 days. The thing is that it only happens to do that when I repeat the function over and over. If I just test -29 days alone, it outputs me the correct 4 weeks value.
Any idea on how can I avoid/fix this?
Edit
This is most likely a bug in PHP.
If I make the code do the division and rounding twice, the NaN never appears.
// do estimate of how many weeks ago
$round = round($days / 7, 0);
$round = round($days / 7, 0);
$date = $round . ' weeks ago';
I noticed this because in my test loops, the first value that triggered the weeks calculation (given that I was making more than one calculation) would give the NaN.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有趣的是,我的黑客也解决了类似的问题。我只是将其放在带有日期的操作之前:
其余的都有效。显然 $y1、$y2 等都是 NAN ......
运行 XAMP、Win 7 x64、DENWER、PHP v 5.3+
interestingly my hack had worked on similar problem. I just put this before the operations with the date:
and the rest worked. Obviously $y1, $y2 etc. are NAN somehow...
Running XAMP, Win 7 x64, DENWER, PHP v 5.3+
虽然我个人认为这是一个错误(我发现一些错误提交和 stackoverflow 问题也有类似的问题),但从 5.3.1 到 5.3.5 后似乎不再发生。
这是一个简化的代码,会给我带来 NaN
(我在 Windows 上运行 XAMPP 1.7.3 (PHP 5.3.1))
While I personally believe this was bug (I found some bug submissions and stackoverflow questions having similar problems) it doesnt seem to be happening anymore after going from 5.3.1 to 5.3.5.
This is a simplified code that would give NaN for me
(I was running XAMPP 1.7.3 (PHP 5.3.1) on Windows)