我有一个网站,允许用户在墙上发帖,他们的帖子是按日期提交的,我会发布一张图片。 我从 GitHub 上获取了日期脚本,它显示发布于 2018-12-03 02:12:33 (示例),我不知道如何将其更改为例如 4 小时前、20 分钟前等。 .我怎样才能做到这一点?
我的comment_list.php
<?php
require_once ("db.php");
$memberId = 1;
$sql = "SELECT
tbl_comment.*,tbl_like_unlike.like_unlike FROM tbl_comment LEFT JOIN tbl_like_unlike ON tbl_comment.comment_id = tbl_like_unlike.comment_id AND member_id = " . $memberId . " ORDER BY tbl_like_unlike.like_unlike DESC, tbl_comment.date DESC";
$result = mysqli_query($conn, $sql);
$record_set = array();
while ($row = mysqli_fetch_assoc($result)) {
array_push($record_set, $row);
}
mysqli_free_result($result);
mysqli_close($conn);
echo json_encode($record_set);
?>
数据库结构
--
-- Table structure for table `tbl_comment`
--
CREATE TABLE `tbl_comment` (
`comment_id` int(11) NOT NULL,
`parent_comment_id` int(11) DEFAULT NULL,
`comment` varchar(200) NOT NULL,
`comment_sender_name` varchar(40) NOT NULL,
`date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
--
-- Table structure for table `tbl_like_unlike`
--
CREATE TABLE `tbl_like_unlike` (
`id` int(11) NOT NULL,
`member_id` int(11) NOT NULL,
`comment_id` int(11) NOT NULL,
`like_unlike` int(2) NOT NULL,
`date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
我的comment_add.php
<?php
require_once ("db.php");
$commentId = isset($_POST['comment_id']) ? $_POST['comment_id'] : "";
$comment = isset($_POST['comment']) ? $_POST['comment'] : "";
$commentSenderName = isset($_POST['name']) ? $_POST['name'] : "";
$date = date('Y-m-d H:i:s');
我在这里阅读了所有类似的问题,但无法帮助自己,希望你能指导我。那么有没有简单的方法可以改变呢?任何帮助都意义重大,谢谢!
最佳答案
最简单的方法是使用 Carbon。查看此链接 https://carbon.nesbot.com/docs/ 。 Carbon 类直接继承自 PHP DateTime 类。例如,
<?php
require 'path/to/Carbon/autoload.php';
use Carbon\Carbon;
//Create instance of Carbon
$date = new Carbon('2018-09-01 03:25:17');
//Then use diffForHumans()
echo $date->diffForHumans();
//Result: 3 months ago
关于javascript - 指导我将日期更改为几小时前,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53624148/