PHP代码似乎不起作用 [英] PHP Code seems to not work
问题描述
我刚刚问了一个问题并得到了一个快速有用的回复,但这段代码似乎在编辑后似乎没有正常运行。
I just asked a question and got a quick and useful response but this code doesn't seem to be functioning correctly after a few edits.
$firstDate = '2013-09-17 21:20:24';
echo $firstDate;
echo '<br>';
$datetime1 = new DateTime($firstDate);
$datetime2 = new DateTime($secondDate);
$interval = $datetime1->diff($datetime2);
if ($interval->a <= 7)
{
if($interval->y !== 0)
{
$elapsed = $interval->format('%y years ago');
}
else
if($interval->m !== 0)
{
$elapsed = $interval->format('%m months ago');
}
else
if($interval->a !== 0)
{
$elapsed = $interval->format('%a days ago');
}
else
if($interval->h !== 0)
{
$elapsed = $interval->format('%h hours ago');
}
else
if($interval->i !== 0)
{
$elapsed = $interval->format('%i minutes ago');
}
else
if($interval->S !== 0)
{
$elapsed = $interval->format('%S seconds ago');
}
$elapsed = str_replace(array('0 years ago', ' 0 months ago', ' 0 days ago', ' 0 hours ago', ' 0 minutes ago'), '', $elapsed);
$elapsed = str_replace(array('1 years ago', ' 1 months ago', ' 1 days ago', ' 1 hours ago', ' 1 minutes ago'), array('1 year ago', '1 month ago', ' 1 day ago', ' 1 hour ago', ' 1 minute ago'), $elapsed);
echo $elapsed;
}
else
{
echo $firstDate;
}
代码应该是给X天前或X秒前的格式
The code is supposed to give the format X days ago or X seconds ago
,但是在几天之后它会停止工作,所以我没有得到几分钟或几秒钟。为什么这不起作用?
but it stops working after days so I don't get minutes or seconds. Why is this not working?
推荐答案
我建议您允许在php.ini文件中查看错误和通知你的开发服务器。
如何在我的开发服务器上启用通知
I would suggest that you allow the viewing of errors and notices in your php.ini file for your development server. How to enable notices on my development server
在运行您提供的代码时,我得到一个注意:未定义的变量:secondDate
在第11行
和未定义的属性:DateInterval :: $ a
在第23行
。
When running the code you provided, I get a Notice: Undefined variable: secondDate
on line 11
, and Undefined property: DateInterval::$a
on line 23
.
您似乎要求PHP使用不完整的数据格式化数字。
It seems you are asking PHP to format a number using incomplete data.
这篇关于PHP代码似乎不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!