Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a friendly date format (for example "submitted 2 days ago") [duplicate]

Possible Duplicate:
How do I calculate relative time?

I want to format dates on my social web app much like Digg.com and other sites do. There, you see very friendly dates, such as:

  • just now
  • 3 minutes ago
  • one hour ago
  • 2 weeks ago
  • 6 months ago
  • etc

Before I wrap my head around creating such a thing, does anyone know of any ready-to-go script for this where I simply insert a datestamp and a friendly date text is given based on how it related to the current time?

PS: I need this in PHP, but pseudo-code or any other language is fine too.

like image 582
Fer Avatar asked Mar 21 '09 22:03

Fer


People also ask

How can I add 1 day to current date?

const date = new Date(); date. setDate(date. getDate() + 1); // ✅ 1 Day added console.

How do I display a date in a specific format?

The dates appear as, mm/dd/yyyy in the U.S. and as, dd/mm/yyyy outside the U.S. where mm is the month, dd is the day, and yyyy is the year. The time is displayed as, hh:mm:ss AM/PM, where hh is the hour, mm is minutes, and ss is seconds.

What is a valid date format?

MM/DD/YY. Month-Day-Year with leading zeros (02/17/2009) 2. DD/MM/YY. Day-Month-Year with leading zeros (17/02/2009)


1 Answers

This is a duplicate of this question. It has a flurry of code samples on how to accomplish this, in addition to the code this very site uses. I glanced at it and there seems to be a PHP implementation posted there too.

In addition to all this, if are you using jQuery you can do this client-side with something like the timeago plugin. It has the advantage of updating the text as time passes so if you load a page and it says "posted 5 minutes ago" and look again 5 minutes later, it says "posted 10 minutes ago"

like image 123
Paolo Bergantino Avatar answered Nov 15 '22 18:11

Paolo Bergantino