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

  • Home
  • SEARCH
  • 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 6123807
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T16:03:35+00:00 2026-05-23T16:03:35+00:00

I am trying to build a function that takes a timestamp and generates relative

  • 0

I am trying to build a function that takes a timestamp and generates “relative” string based date, possibly a reverse strtotime!

Yesterday at 2pm
Yesterday at 3:15pm
Earlier Today at 9am
Today at 12pm
Today at 12:45pm
Tomorrow at 5pm
Monday at 9pm
Sunday at 12:55am
All dates older than a week:
Thu 7th Jul

I currently have the below code, and was wondering how to improve it? It doens’t seem very efficient and is missing some features. What would you add?

  static public function timestampRelativeString($timestamp, $rounding=900) {
    if(!self::isValidTimeStamp($timestamp))
      return "Invalid Timestamp";

    //Round to (default) 15 mins
    $timestamp = floor($timestamp/$rounding)*$rounding;

    //Today
    if($timestamp<mktime(0, 0, 0, date("m"), date("d")-1, date("y")))
      return date("D jS M", $timestamp);
    elseif($timestamp<mktime(23, 59, 59, date("m"), date("d")-1, date("y")))
      return "Today at ".date("G:i", $timestamp);
    elseif($timestamp<time()))
      return "Earlier today at ".date("G:i", $timestamp);
    elseif($timestamp<mktime(23, 59, 59, date("m"), date("d"), date("y")))
      return "Today at ".date("G:i", $timestamp);
    elseif($timestamp<mktime(23, 59, 59, date("m"), date("d")+1, date("y"))) //Tomorrow
      return "Tomorrow at ".date("G:i", $timestamp);
    elseif($timestamp<mktime(23, 59, 59, date("m"), date("d")+7, date("y"))) //Next Week
      return "On ".date("l", $timestamp)." at ".date("G:i", $timestamp);
    else 
      return date("D jS M", $timestamp);
  }

Does anyone know of a script that already does this? Or very similar?

Many thanks for your time,

  • 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-23T16:03:36+00:00Added an answer on May 23, 2026 at 4:03 pm

    This has many names including nicetime and PrettyDate. The best way to do it is to output the actual time from PHP in a div/span with an ID or identifying class, and then post-process client side with javascript (which can then update the time as the user sits on the page).

    Anyway, WRT to your question, I’d look at the prettydate processing classes out there, which are like (or exactly) the ones you see on Facebook, Twitter etc.. even if you do it in PHP you can at match the functionality.

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

Sidebar

Related Questions

I am trying to build a simple worker function that takes a config of
I'm trying to build a polynomial function generator, so that it takes a vector
I'm trying to build a function that will show me all path's where a
I'm trying to build a function that calculates hashed for a file. The function
I'm trying to build a markov generator that takes an arbitrary length for the
I am trying to build a function in php where it takes the user
I'm trying to write a function that takes a 2D vector as an argument.
I'm trying to create a template function that takes a vector argument of any
I'm trying to build a python class that parses a string and if it
I am trying to build a function in C/C++ to sort an array and

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.