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 6768381
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T15:04:07+00:00 2026-05-26T15:04:07+00:00

function RelativeTime($timestamp) { $difference = time() – $timestamp; $periods = array( sec, min, hour,

  • 0
function RelativeTime($timestamp) {
    $difference = time() - $timestamp;
    $periods    = array(
        "sec", "min", "hour", "day", "week", "month", "years", "decade"
    );
    $lengths    = array("60", "60", "24", "7", "4.35", "12", "10");

    if ($difference > 0) { // this was in the past
        $ending = "ago";
    } else { // this was in the future
        $difference = -$difference;
        $ending     = "to go";
    }
    for ($j = 0; $difference >= $lengths[$j]; $j++)
        $difference /= $lengths[$j];
    $difference = round($difference);
    if ($difference != 1) $periods[$j] .= "s";
    $text = "$difference $periods[$j] $ending";
    return $text;
}

I found the above PHP function on the interwebs. It seems to be working pretty well, except it has issues with dates far in the future.

For example, I get looping PHP error

division by zero

for $difference /= $lengths[$j]; when the date is in 2033.

Any ideas how to fix that? The array already accounts for decades, so I am hoping 2033 would result in something like “2 decades to go”.

  • 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-26T15:04:07+00:00Added an answer on May 26, 2026 at 3:04 pm

    The problem is that the second array $lengths contains 7 elements so when executing the last iteration of the loop (after deviding by 10 – for the decades) $j = 7, $lengths[7] is undefined, so converted to 0 and therefore the test $difference >= $lengths[$j] returns true. Then the code enters an infinite loop. To overcome this problem, just add one more element to the $lengths array, say “100”, so the for loop to terminate after processing the decades. Note that dates can be represented in UNIX timestamp if they are before January 19, 2038. Therefore you cannot calculate dates in more than 4 decates so 100 is sufficiant to break from the loop.

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

Sidebar

Related Questions

function returnsAnArray () { return array ('test'); } echo returnsAnArray ()[0]; generates a syntax
function register_contact ($person = array()) { $nogood = false; foreach ($person as $val) {
Any VB/Javascript guru out there that can convert this for me? Protected Function RelativeTime(ByVal
I have this function to work out the relative time that has elapsed since
The pthread_cond_timedwait function needs an absolute time in a time timespec structure. What time
function simple_form($form_state) { $form['item'] = array('#type' => 'hidden', '#value' => 'some_value'); $form['#action'] = 'http://external-website.com/';
function heaviside(&$value, $key, &$array) { if($key > 0) $value = $array[$key-1].$array[$key]; } function test_heaviside()
function a($function, $array) { global $test $test->$function(implode(',' $array)); } For example, I want to
function timeSpent($date_created, $endingDay) { $diff = abs(strtotime($date_created) - strtotime(date(Y-m-d H:i:s))); $years = floor($diff /
Function FillAdminAccount() As Boolean FillAdminAccount = True Try SQLconn.ConnectionString = connect timeout=9999999; & _

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.