我的时间计算不正确我有一个在profile.php中调用的time.php来计算上次发送邮件等等
我有以下行调用行时间函数time_passed
echo "time_passed"($row['time']);
这是用于计算时间的CODE
<?php
// DISPLAYS COMMENT POST TIME AS "1 year, 1 week ago" or "5 minutes, 7 seconds ago", etc...
function time_passed($timestamp){
//type cast, current time, difference in timestamps
$timestamp = (int) $timestamp;
$current_time = time();
$diff = $current_time - $timestamp;
//intervals in seconds
$intervals = array (
'year' => 31556926, 'month' => 2629744, 'week' => 604800, 'day' => 86400, 'hour' => 3600, 'minute'=> 60
);
//now we just find the difference
if ($diff == 0)
{
return 'just now';
}
if ($diff < 60)
{
return $diff == 1 ? $diff . ' second ago' : $diff . ' seconds ago';
}
if ($diff >= 60 && $diff < $intervals['hour'])
{
$diff = floor($diff/$intervals['minute']);
return $diff == 1 ? $diff . ' minute ago' : $diff . ' minutes ago';
}
if ($diff >= $intervals['hour'] && $diff < $intervals['day'])
{
$diff = floor($diff/$intervals['hour']);
return $diff == 1 ? $diff . ' hour ago' : $diff . ' hours ago';
}
if ($diff >= $intervals['day'] && $diff < $intervals['week'])
{
$diff = floor($diff/$intervals['day']);
return $diff == 1 ? $diff . ' day ago' : $diff . ' days ago';
}
if ($diff >= $intervals['week'] && $diff < $intervals['month'])
{
$diff = floor($diff/$intervals['week']);
return $diff == 1 ? $diff . ' week ago' : $diff . ' weeks ago';
}
if ($diff >= $intervals['month'] && $diff < $intervals['year'])
{
$diff = floor($diff/$intervals['month']);
return $diff == 1 ? $diff . ' month ago' : $diff . ' months ago';
}
if ($diff >= $intervals['year'])
{
$diff = floor($diff/$intervals['year']);
return $diff == 1 ? $diff . ' year ago' : $diff . ' years ago';
}
}
?>
结果如下
我的桌子是
时间按以下格式插入
运行查询时会自动插入时间,然后我会在上面的表格中显示结果,但46年前就说明了,可能导致这种情况的原因是什么?感谢
答案 0 :(得分:2)
您需要执行以下操作
将此行$timestamp = (int) $timestamp;
更改为
$timestamp = strtotime($timestamp);
并添加您所在国家/地区的默认时间。例如,因为我来自印度,我曾经在函数的开头设置date_default_timezone_set('Asia/Kolkata');