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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T09:28:05+00:00 2026-05-13T09:28:05+00:00

I’ve run into a strange timewarp while doing some math with time, and it

  • 0

I’ve run into a strange timewarp while doing some math with time, and it has left me stumped. Or, well, I’ve found the reason (or atleast a plausible one), but don’t quite know what to do about it, or if there is indeed anything that can be done.

The issue in plain words is, that when adding time in larger units than 1 week (it’s multipliers excluded) it seems to be impossible to be exact. And by exact I mean, that when I add 1 years worth of seconds to NOW, I end up 1 year and some hours from NOW.

I can understand that adding 1 (or more) months will do this, as a months length varies, but a year should be more or less the same, shouldn’t it?

Now I know you’ll want to know how I do this, so here follows (pseudoish) code:

class My_DateInterval {
    public $length; // Interval length in seconds

    public function __construct($interval) {
        $this->length = 0;

        preg_match(
            '/P(((?<years>([0-9]{1,}))Y)|((?<months>([0-9]{1,}))M)|((?<weeks>([0-9]{1,}))W)|((?<days>([0-9]{1,}))D)){0,}(T((?<hours>([0-9]{1,2})){1}H){0,1}((?<minutes>([0-9]{1,2}){1})M){0,1}((?<seconds>([0-9]{1,2}){1})S){0,1}){0,1}/', 
            $interval, $timeparts
        );

        if (is_numeric($timeparts['years'])) $this->length += intval($timeparts['years']) * 31556926; // 1 year in seconds
        if (is_numeric($timeparts['months'])) $this->length += intval($timeparts['months']) * 2629743.83; // 1 month in seconds
        if (is_numeric($timeparts['weeks'])) $this->length += intval($timeparts['weeks']) * 604800; // 1 week in seconds
        if (is_numeric($timeparts['days'])) $this->length += intval($timeparts['days']) * 86400; // 1 day in seconds
        if (is_numeric($timeparts['hours'])) $this->length += intval($timeparts['hours']) * 3600; // 1 hour in seconds
        if (is_numeric($timeparts['minutes'])) $this->length += intval($timeparts['minutes']) * 60; // 1 minute in seconds
        if (is_numeric($timeparts['seconds'])) $this->length += intval($timeparts['seconds']);

        $this->length = round($this->length);
    }
}

class My_DateTime extends DateTime {
    public function __construct($time, $tz = null) {
        parent::__contruct($time, $tz);
    }

    public function add(My_DateInterval $i) {
        $this->modify($i->length . ' seconds');
    }
}

$d = new My_DateTime();
$i = new My_DateInterval('P1Y');
$d->add($i);

Doing some debug printouts of the interval length and before/after values show that it’s all good, in the sense that “it works as expected and all checks out”, but the issue stated above still stands: there is an anomaly in the exactness of it all which I’d very much would like to get right, if possible.

In so many words: How to do exact mathematics with time units greater than 1 week. (I.e. 1 month / 1 year).

PS. For those wondering why I’m using My_* classes is because PHP 5.3 just isn’t widespread enough, but I’m trying to keep things in a way that migrating to built-in utility classes will be as smooth as possible.

  • 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-13T09:28:06+00:00Added an answer on May 13, 2026 at 9:28 am

    A year is 365.25 days, roughly. Hence we have leap years.

    Months have variable lengths.

    Hence the semantics of what adding a year and adding a month probably don’t correspond to adding a fixed number of seconds.

    For example adding a month to 14th Feb 2007 would probably be expected to yield 14th March 2007 and to 14th Feb 2008 would ive 14th March 2008, adding 28 or 29 days respectively.

    This stuff gets gnarly, especially when we add in different calendars, much of the world doesn’t even have a February! Then add “Seven Working Days” – you need to take a public holiday calendar into account.

    Are there no libraries you can find for this?

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

Sidebar

Ask A Question

Stats

  • Questions 314k
  • Answers 314k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer If you have too many clauses, performance will suffer, especially… May 13, 2026 at 10:58 pm
  • Editorial Team
    Editorial Team added an answer When treated as a boolean, 0 is considered to be… May 13, 2026 at 10:58 pm
  • Editorial Team
    Editorial Team added an answer This works for me (turns the led on and off… May 13, 2026 at 10:58 pm

Related Questions

I've got a string that has curly quotes in it. I'd like to replace
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I have a French site that I want to parse, but am running into
I have text I am displaying in SIlverlight that is coming from a CMS

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.