PHP中计算时差的问题
我有一个脚本,用于检查将条目添加到数据库中的日期和时间,将其与今天的日期和时间进行比较,并以最相关的单位(分钟、小时、天、周等)输出差异。
问题是任何超过一周的条目仅显示为“6 天前添加”。
显然,我计算时差的方式存在一些问题,但我似乎无法识别它。
这是我的代码:
$date_time_added = $date_added . $time_added;
$current_date_time = $current_date . $current_time;
// Check how many X ago comment was added
$diff = abs(strtotime($current_date_time) - strtotime($date_time_added));
$years = floor($diff / (365*60*60*24));
$months = floor(($diff - $years * 365*60*60*24) / (30*60*60*24));
$days = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24));
$hours = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24 - $days*60*60*24)/ (60*60));
$minutes = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24 - $days*60*60*24 - $hours*60*60)/ 60);
$seconds = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24 - $days*60*60*24 - $hours*60*60 - $minuts*60));
// Check if comment was added in last 24 hours
if ($days < '1') {
// Check if comment was added in the last hour
if($hours == 0) {
// Check if comment wass added in the last minute
if ($minutes == 0) {
$when = 'Posted ' . $seconds . ' seconds ago';
} else {
$when = 'Posted ' . $minutes . ' minutes ago'; }
} else {
$when = 'Posted ' . $hours . ' hours ago';
}
} else {
$when = 'Posted ' . $days . ' days ago';
}
感谢您的帮助。
I have a script which checks the date and time an entry was added to the database, compares it to todays date and time, and outputs the difference in the most relevant unit (minutes, hours, days, weeks etc).
The problem is that any entries that are over a week old only display as 'added 6 days ago.'
I obviously have some problem in the way that I'm calculating the time difference but I can't seem to identify it.
Here's my code:
$date_time_added = $date_added . $time_added;
$current_date_time = $current_date . $current_time;
// Check how many X ago comment was added
$diff = abs(strtotime($current_date_time) - strtotime($date_time_added));
$years = floor($diff / (365*60*60*24));
$months = floor(($diff - $years * 365*60*60*24) / (30*60*60*24));
$days = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24));
$hours = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24 - $days*60*60*24)/ (60*60));
$minutes = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24 - $days*60*60*24 - $hours*60*60)/ 60);
$seconds = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24 - $days*60*60*24 - $hours*60*60 - $minuts*60));
// Check if comment was added in last 24 hours
if ($days < '1') {
// Check if comment was added in the last hour
if($hours == 0) {
// Check if comment wass added in the last minute
if ($minutes == 0) {
$when = 'Posted ' . $seconds . ' seconds ago';
} else {
$when = 'Posted ' . $minutes . ' minutes ago'; }
} else {
$when = 'Posted ' . $hours . ' hours ago';
}
} else {
$when = 'Posted ' . $days . ' days ago';
}
Thanks for any help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
为什么计算月份时要把年份去掉?如果您只需要最相关的单位,这不会做到这一点:
Why do you take the years out when you calculate the months? If you only need the most relevant unit wouldn't this do it:
将
floor()
更改为round()
。另外,您在上次作业中拼错了$mines
。Change
floor()
toround()
. Also, you misspelled$minutes
in the last assignment.我最终在我的一个项目中使用了加雷斯的答案。这是一个修改版本,将为您提供 1 天或 1 个月,而不是 1 天或 1 个月。谢谢加雷斯!
I ended up using Gareth's answer in one of my projects. Here is a modified version that will give you 1 day or 1 month instead of 1 days or 1 months. Thanks Gareth!