我有一个网站,允许用户在墙上张贴帖子,并且他们的帖子按日期提交,我将发布图片。 我从GitHub上获取了日期脚本,该脚本显示在2018-12-03 02:12:33(示例)上,并且我不知道如何将其更改为例如4h之前,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');
我已经在这里阅读了所有类似的问题,但无法帮助自己,希望您能引导我。有没有简单的方法可以更改它?任何帮助都将非常重要,谢谢!
答案 0 :(得分:0)
最简单的方法是使用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