Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

DavidBuchukuri's avatar

Convert eloquant's default timestamps to human readable format

I am developing app with vue spa and laravel api and I need to convert timestamps to human readable format.I have used diffForHumans() before, and I want to get similar result, but on spa side. Is there a way in js to convert a string like this 2022-07-20T10:46:12.000000Z to something like 15 minutes ago

0 likes
6 replies
hcastillo's avatar

Hi, if you are using JS, then, can use this function. In my case using locate as spanish

const convertDate = (date) => {
  return new Date(date).toLocaleDateString("es-ES", {
    weekday: "long",
    year: "numeric",
    month: "long",
    day: "numeric",
  });
};
1 like
Sinnbeck's avatar

Or you can use api resources in laravel, and in there format the date using ->diffForHumans()

1 like
Snapey's avatar

moment.js seems to be the usual way

1 like
DavidBuchukuri's avatar
DavidBuchukuri
OP
Best Answer
Level 4

@sinnbeck that would work, but I am doing notifications popup where user sees his notifications, and time difference. So I think in my case fetching notifications whenever user clicks on that button wouldn't be good for performance. however I came up with my own function which did the trick.

  timeDiff() {
      let laravelTime = new Date(this.notification.created_at);
      let nowTime = new Date();

      var diff = Math.abs(nowTime - laravelTime);

      var minutes = Math.floor(diff / 1000 / 60);

      if (minutes >= 60 && minutes <= 1440) {
        let time = Math.floor(minutes / 60);
        if (time === 1) {
          return time + " hour ago";
        }
        return time + " hours ago";
      }

      if (minutes < 60) {
        let time = Math.floor(minutes);
        if (time === 1) {
          return time + " minute ago";
        }
        return time + " minutes ago";
      }

      let time = Math.floor(minutes / 1440);

      if (time === 1) {
        return time + " day ago";
      }
      return time + " days ago";
    }

Please or to participate in this conversation.