Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6824511
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:54:36+00:00 2026-05-26T21:54:36+00:00

What i’m trying to do is show date in more readable format like 3

  • 0

What i’m trying to do is show date in more readable format like 3 seconds ago, 1 hours ago, yesterday, June 12 and etc.
What i did is , first calculate the timestamp for comment’s date.. Then send it to client with ajax, then create two date instance on javascript one for is comment’s date and other one is current date.. After that I just find differences between to date item then write on the screen with fancy words..
Everything works fine on localhost, even server.. But sometimes if client’s pc date is earlier from the server date (independent from time zone)..
Let say server time is today 13.30 pm and client time is today 13.00 , it’s failing with this scenario because current time is being comment’s post time.. Difference will be negative value..
I’m creating date object for comment in php like this;

date("Y-m-d G:i:s")

Then write it to mysql db..
After that when i select comments i convert it to timestamp to push it on client side with ;

$comment['timestamp'] = strtotime($row['creationDate']);

And then in javascript, I make calculation for human readable date format ;

DateObject.getFormatted = function(unixtime){

d = new Date(unixtime*1000);
now = new Date();

var diff = (now.getTime()/1000- unixtime) ;
var result = "";

var MIN = 60,
    HOUR = 3600,
    DAY = 86400;

if(diff < 2)
    result ="just now";
else if(diff < MIN)
    result = Math.floor(diff) + " seconds ago";
else if(diff < HOUR)
    result = Math.floor(diff/60) + " minutes ago"; 
else if(diff < DAY)
    result = Math.floor(diff/3600) + " hours ago";
else if(diff < DAY*3)
{   
    var days = diff/DAY;
    if(days < 2)
        result = "yesterday";
    else
        result = Math.floor(days) + " days ago"; 
}
else if(now.getFullYear() == d.getFullYear())
{
    formattedTime = this.getTime(d);
    result = this.getSameYear(d) + " at " + formattedTime;
}
else
{
    formattedDate = this.getDate(d); 
    formattedTime = this.getTime(d);
    result = formattedDate + " at " + formattedTime;
}       

return result;




};

So if clients date is earlier then the comment’s date diff value is being negative, so the first case would be true

if(diff < 2)
result ="just now";

It’s going behing as time difference between client and comment’s date .. In my case 10 minutes.. If I set my computer time 10 minutes later it’s working nice..
So how can i fix it in a better way?

Thank you.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T21:54:37+00:00Added an answer on May 26, 2026 at 9:54 pm

    May be better send from server to client difference between comment date and current date?(instead of creation two javascript date instances)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.