-2

我目前正在制作一个发表评论系统。

当用户对帖子发表评论时,我想将日期和时间存储在数据库中。现有评论应显示自添加评论以来经过多长时间的时差。

即:2 分钟前、3 天前等 - 类似于 Facebook 上的情况。

SQL 或 PHP 中是否有任何方法或属性来处理这个问题?

哪种 SQL 数据类型适合此目的?日期时间或时间戳?

4

2 回答 2

1

MySQL 中的时间戳一般用于跟踪记录的更改,并且经常在每次记录更改时更新。如果要存储特定值,则应使用日期时间字段。如果您的意思是要在使用 UNIX 时间戳或原生 MySQL 日期时间字段之间做出决定,请使用原生格式。您可以通过这种方式在 MySQL 中进行计算(“SELECT DATE_ADD(my_datetime, INTERVAL 1 DAY)”),并且在查询记录时将值的格式更改为 UNIX 时间戳(“SELECT UNIX_TIMESTAMP(my_datetime)”)很简单如果你想用PHP对其进行操作。一个重要的区别是 DATETIME 表示日期(在日历中)和时间(可以在挂钟上观察到),而 TIMESTAMP 表示明确定义的时间点。如果您的应用程序处理时区,这可能非常重要。'2010-09-01 16:31:00' 是多久以前的事了?这取决于您所在的时区。对我而言,这只是几秒钟前,对您而言,它可能代表未来的某个时间。如果我说自 '1970-01-01 00:00:00 UTC' 以来的 1283351460 秒,那么您确切知道我所说的时间点。

差异用途:

$timeFirst  = strtotime($postDateTime);
$timeSecond = strtotime($commentDateTime);
$differenceInSeconds = $timeSecond - $timeFirst;

要显示时间,请使用:

function relativedate($secs) {
        $second = 1;
        $minute = 60;
        $hour = 60*60;
        $day = 60*60*24;
        $week = 60*60*24*7;
        $month = 60*60*24*7*30;
        $year = 60*60*24*7*30*365;

        if ($secs <= 0) { $output = "now";
        }elseif ($secs > $second && $secs < $minute) { $output = round($secs/$second)." second";
        }elseif ($secs >= $minute && $secs < $hour) { $output = round($secs/$minute)." minute";
        }elseif ($secs >= $hour && $secs < $day) { $output = round($secs/$hour)." hour";
        }elseif ($secs >= $day && $secs < $week) { $output = round($secs/$day)." day";
        }elseif ($secs >= $week && $secs < $month) { $output = round($secs/$week)." week";
        }elseif ($secs >= $month && $secs < $year) { $output = round($secs/$month)." month";
        }elseif ($secs >= $year && $secs < $year*10) { $output = round($secs/$year)." year";
        }else{ $output = " more than a decade ago"; }

        if ($output <> "now"){
            $output = (substr($output,0,2)<>"1 ") ? $output."s" : $output;
        }
        return $output;
    }



echo relativedate(60); // 1 minute

它将完全像在脸上一样显示。您必须在几秒钟内将评论和帖子之间的差异传递给该函数。

于 2013-06-18T17:52:36.850 回答
0

我只是想改进君的答案。在我尝试在我的代码上实现这个之后,Jun 在他的代码上的计算有点不正确,在月份和年份计算中

这是我的代码

function relativedate($secs) {
    $second = 1;
    $minute = 60;
    $hour = 60*60;
    $day = 60*60*24;
    $week = 60*60*24*7;
    $month = 60*60*24*7*4;         // because 1 month is 4 week
    $year = 60*60*24*7*4*12;       // because 1 year is 12 month

    if ($secs <= 0) { $output = "now";
    }elseif ($secs > $second && $secs < $minute) { $output = round($secs/$second)." second";
    }elseif ($secs >= $minute && $secs < $hour) { $output = round($secs/$minute)." minute";
    }elseif ($secs >= $hour && $secs < $day) { $output = round($secs/$hour)." hour";
    }elseif ($secs >= $day && $secs < $week) { $output = round($secs/$day)." day";
    }elseif ($secs >= $week && $secs < $month) { $output = round($secs/$week)." week";
    }elseif ($secs >= $month && $secs < $year) { $output = round($secs/$month)." month";
    }elseif ($secs >= $year && $secs < $year*10) { $output = round($secs/$year)." year";
    }else{ $output = " more than a decade ago"; }

    if ($output <> "now"){
        $output = (substr($output,0,2)<>"1 ") ? $output."s" : $output;
    }
    return $output;
}

echo relativedate(60); // 1 minute
于 2016-01-15T03:51:38.677 回答