douzhi9635 2018-12-05 02:09
浏览 71

指导我将日期更改为小时前[重复]

This question already has an answer here:

I have a website letting users posts on a wall and their posts are being submitted by date, I will post a picture. I took the the date script from GitHub, it's showing posted at 2018-12-03 02:12:33 (example), and I don't know how to change it to for example to 4h ago, 20 minutes ago etc... How can I achieve that?

My 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);
    ?>

Database structure

    --
    -- 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;

My 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');

I've read all similar questions here but coulnd't help myself, hope you could guide me. So is there a easy way changing it? Any help will mean a lot, thanks!

How date displays on my posts

</div>
  • 写回答

1条回答 默认 最新

  • dongqishun6409 2018-12-05 02:16
    关注

    The easiest way to do this is to use Carbon. Checkout this link https://carbon.nesbot.com/docs/. The Carbon class is inherited directly from PHP DateTime Class. For example,

    <?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
    
    评论

报告相同问题?

悬赏问题

  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式
  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错