0

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

Zli
  • 107
  • 1
  • 2
  • 8
  • IdontDownVote dude please I said that I already checked it and I didnt know how to do it, and I hope someone can guide me... – Zli Dec 05 '18 at 02:21

1 Answers1

0

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
Renzchler
  • 321
  • 1
  • 5
  • 16
  • link only answers are discouraged –  Dec 05 '18 at 02:16
  • Thank you for correcting me @IdontDownVote – Renzchler Dec 05 '18 at 02:25
  • So how do I actually do it? With my code? What do I replace? I never faced dates before and I'm kinda new to php, thanks @Ranzchler – Zli Dec 05 '18 at 02:30
  • First, you have to install via composer or you can download it directly. I suggest you download it if your not familiar with composer. Then require the Carbon /pathto/autoload.php in your php file. You can now then use the methods within the Carbon class. You can visit the link provided on how to use it step by step and also there are lots of examples. – Renzchler Dec 05 '18 at 02:34
  • Do you want to do it for me? So I can see it actually? I've been trying it for 2 weeks everyday and I don't know why it's not working. I can pay you also I'm desperate, this is the last thing on my project and I thought it will be easy – Zli Dec 05 '18 at 02:48
  • Sorry @Zli but I can't do it for you. It is your project and I know it is very important. I know its hard, I've been there. Just take the pressure I believe you can do it! Checkout this tutorial on Youtube [link](https://www.youtube.com/watch?v=3dTkvY5eyqw&t=12s) . And always practice what you learn everyday. – Renzchler Dec 05 '18 at 03:10