Have you ever wonder how many seconds do you already spent in your life? And minutes? And hours? And days? In this article we're going to explain the operations of the script that we developed to calculate the length of our lives in days, hours, minutes and seconds.

Premise

The datas available are year, month, day, hours, minutes, seconds, date of birth and the current date.

  • It takes for granted that the input's datas are related to real situations and the date of birth must precede the current one.
  • The algorithm is valid for dates of birth of not less than January 1st 1900 at 00:00:00, and the software can provide correct results until March 1, 2100 at 00:00:00. To manage the dates out side this interval additional considerations should make. However, this was avoided for obvious reasons.

The Algorithm

The age in seconds is obtained by calculating the seconds from January 1, 1900 at 00:00:00 both to the date of birth, and to the current date and making the difference between these two values.

CALCULATING THE SECONDS FROM 01/01/1900 00:00:00

To calculate the seconds since 01/01/1900 00:00:00 for a determinated date d / m / y - h : i : s you need to add the seconds difference due to:

  1. The years passed;
  2. Leap years passed;
  3. The month where we are;
  4. The day when we are;
  5. The hour when we are;
  6. The minutes where we are;
  7. The seconds where we are.

Then you have to subtract the second day if we are in January or February of a leap year (the reason is explained in the second point below).

  1. 'years since 1900' ∙ 'seconds in a non-leap year' = (y - 1900) ∙ 31536000
  2. 'Leap years past, including the current one if it is a leap year' ∙ 'seconds in a day' == 'integer part of the division of years since 1900 and 4' ∙ 'seconds in a day' == ('quotient of this division' - 'the remainder of this division' / 4) ∙ 'seconds in a day' == ((y - 1900) / 4 - (y mod 4) / 4) ∙ 86400 = (y - 1900 - y mod 4) ∙ 21600
    The effects of any current leap year (an increase of one day) if they do not occur in January or February. Therefore you have to remove 86400 if you are in this situation.
  3. You need to know the days elapsed from January 1 of the current year to the first day of the current month (since now I will indicate with f). f depends on the month in which we:
    • If it's January (m = 1): f = 0;
    • If it is January (m = 2): f = 31;
    • If it is January (m = 3): f = 59;
    • If it is in April (m = 4): f = 90;
    • If you may (m = 5): f = 120;
    • If it is in June (m = 6): f = 151;
    • If it is July (m = 7): f = 181;
    • If it is August (m = 8): f = 212;
    • If it is January (m = 9): f = 243;
    • If it's October (m = 10): f = 273;
    • If it is January (m = 11): f = 304;
    • If it's December (m = 12): f = 334;
    then:
    f ∙ 'seconds in a day' = 86400 f
  4. 'days elapsed since the first day of the current month' ∙ 'seconds in a day'= (d - 1) ∙ 86400
  5. 'hours passed from the beginning of the day' ∙ 'seconds in an hour' = 'current hours' ∙ 'seconds in an hour' = 3600 h
  6. 'minutes passed from the beginning of the hour' ∙ 'seconds in a minute' = 'current minutes' ∙ 'seconds in a minute' = 60 i
  7. 'seconds passed from the beginning of the minute' = 'current seconds' = s

Assign to g the value 86400 only if we are in January or February of a leap year or if and only if:

(m = 1 ∨ m = 2) ∧ (y mod 4 = 0) ∧ (y ≠ 1900)

otherwise we give the value 0. Moreover, we give the value 3600 to j if the hour is legal (otherwise we give the value 0).
The seconds since 01/01/1900 00:00:00 for a date are, ultimately, the sum of the seven points discussed above with prudence relative to a, g and a, j:

s1900 = (y - 1900) ∙ 31536000 + (y - 1900 - y mod 4) ∙ 21600 + 86400 f + (d - 1) ∙ 86400 + 3600 h + 60 i + s - g - j

The same applies to the date of birth D / M / Y - H : I : S (G and J):

S1900 = (Y - 1900) ∙ 31536000 + (Y - 1900 - Y mod 4) ∙ 21600 + 86400 F + (D - 1) ∙ 86400 + 3600 H + 60 I + S - G - J

CALCULATING THE SECONDS, MINUTES FROM THE DATE OF BIRTH ... etc.

Finally, the seconds passed since the date of birth (which is what we're looking for from the beginning) are:

Seconds passed since the date of births = 1900 - S1900

 

To get the minutes, hours, days of age just divide the seconds of age, respectively: 60, 3600 e 86400.