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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T21:43:06+00:00 2026-06-07T21:43:06+00:00

I have a todo list app I’m working on. It displays the timestamp from

  • 0

I have a todo list app I’m working on. It displays the timestamp from when a todo was created and even allows the user to choose what the format should be.

In this snippet, the code grabs info from a mySQL db to see how to format the date:

public function __toString(){
    // The string we return is outputted by the echo statement

    if ( $this->data['date_created'] == '') {
        $date_created =  date($GLOBALS["config"]["date_format"].' '.$GLOBALS["config"]["time_format"]);
    }
    else 
        $date_created = date($GLOBALS["config"]["date_format"].' '.$GLOBALS["config"]["time_format"], strtotime($this->data['date_created']));

(It’s pulling from a row called date_format and a row called time_format)

I’m trying to implement this code:

function relativeTime($dt,$precision=2) {
    if(is_string($dt)) $dt = strtotime($dt);

    $times=array(   365*24*60*60    => "year",
                    30*24*60*60     => "month",
                    7*24*60*60      => "week",
                    24*60*60        => "day",
                    60*60           => "hour",
                    60              => "minute",
                    1               => "second");

    $passed=time()-$dt;

    if($passed<5)
    {
        $output='less than 5 seconds ago';
    }
    else
    {
        $output=array();
        $exit=0;

        foreach($times as $period=>$name)
        {
            if($exit>=$precision || ($exit>0 && $period<60)) break;

            $result = floor($passed/$period);
            if($result>0)
            {
                $output[]=$result.' '.$name.($result==1?'':'s');
                $passed-=$result*$period;
                $exit++;
            }
            else if($exit>0) $exit++;
        }

        $output=implode(' and ',$output).' ago';
    }

    return $output;
}

I replaced $dt with $date_created and tried different combos, but I can’t seem to get it right. I have a frontend error telling me that relativetime is undefined (it doesn’t say relativeTime in error message, it has a lowercase “t”). What can I do to get $date_created to show up in the “Days ago” format? Thanks!

  • 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-06-07T21:43:08+00:00Added an answer on June 7, 2026 at 9:43 pm

    I would rewrite this completely as something like this:

    $times = array(365*24*60*60    => "year",
                   30*24*60*60     => "month",
                   7*24*60*60      => "week",
                   24*60*60        => "day",
                   60*60           => "hour",
                   60              => "minute",
                   1               => "second");
    
    $passed = time() - $dt;
    
    if ($passed < 5) {
        echo "Less than 5 seconds ago.";
    } else {
        foreach ($times as $period => $name) {
            if ($passed >= $period) {
                echo round($passed / $period) . " " . $name . "(s) ago";
                break;
            }
        }
    }
    

    See this link for an example (I’ve hardcoded the value of $passed for testing purposes; you can edit the value to see how it affects things):

    http://ideone.com/e8KXK


    As an aside, I’ve realized that I think your original solution attempts to fully account for every second that has passed. This is a bad idea from a user experience perspective. I don’t think many users will want to see “Created 1 year and 7 months and 19 days and 4 hours and 36 minutes and 28 seconds ago”, which is the type of result that your original code was shooting for. This answer will only display the largest unit.

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

Sidebar

Related Questions

Let's say I have an app that handles a TODO list. The list has
In my app, I have a task list (no, it's not just another todo
I have created an app that I want to list all the installed applications
I have an app that is a list of tasks, like a to do
I have a list of TODO items, and I'm trying to have 2 TODO
I have a problem with my android-app. I want to save a Data-list in
im making a todo list app in php and im using unordered lists to
I'm using django-taggit to tag items in a todo list app. I'm trying to
The Git Ropo for full code I have defined: /app/model/Todo: Ext.define('Test.model.Todo', { extend: 'Ext.data.Model',
Am following the tutorial here on creating a simple todo list app: http://www.mydroid.apnafundaz.com/2010/07/todolist-application-in-android-step-by-step-guide-with-screen-shots/ Have

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.