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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T08:16:45+00:00 2026-06-11T08:16:45+00:00

I have borrowed beautiful Cokidoo DateTime class, that converts regular DateTime to ago format.

  • 0

I have borrowed beautiful Cokidoo DateTime class, that converts regular DateTime to “ago format”.

I’m developing multilingual web-site and wanted to replace year ago, years ago, month ago with variables.

Code example:

protected $strings = array(
        'y' => array('1 year ago', '%d years ago'),
        'm' => array('1 month ago', '%d months ago'),
        'd' => array('1 day ago', '%d days ago'),
        'h' => array('1 hour ago', '%d hours ago'),
        'i' => array('1 minute ago', '%d minutes ago'),
        's' => array('now', '%d secons ago'),
    );

Is this possible to do this without using replace methods?

Obviously this didn’t work:

'm' => array('1' . $month_ago, '%d' . $months_ago),

Any help please?

  • 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-11T08:16:46+00:00Added an answer on June 11, 2026 at 8:16 am

    That class would be much more useful if it did not extend DateTime. The only thing it does is format a date, it has no reason at all to be a date. It could just as easily create a DateTime on demand and work on that instead of $this.

    If you make this modification (and appropriately rename the class to e.g. DateTimeDiffFormatter) then suddenly all the possibilities are open: you can pass a parameter to the constructor that determines the language to be used, or even better pass in some reference to your i18n component. Then for example you could have

    // I have no idea why this was protected, and probably the class author did not as well
    private $strings = array(
        'y' => array('years_ago_1', 'years_ago_n'),
        // etc etc
    );
    

    and

    return sprintf($localizer->get_string($this->strings[$intervalKey][$pluralKey]),
                   $value);
    

    Finally, it’s a really bad idea to assume that you can hardcode the format of the localized strings to “X minutes ago” and similar. The number needs to be part of the format string since in many cultures it does not precede the “how long ago” part.

    Update

    I adapted Ilia’s candidate solution to bring it in line with the suggestions above; the result is here.

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

Sidebar

Related Questions

I have a class that I borrowed from here that loads and image from
I have some code that I borrowed from a long while ago that sets
I have some javascript code (that I borrowed from BalusC) that is used to
So I have a method that I borrowed from the internet that I use
This is borrowed from another question I made on the site, but would have
I have borrowed code from this link PHP regex templating - find all occurrences
have written this little class, which generates a UUID every time an object of
I borrowed Agile Web Development with Rails from my local library, and for Mac
I have several hand-written Java classes in a REST Server that uses JAXB to
I have an Ember.js app as follows (borrowed from this article on andyMatthews.net): View:

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.