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.
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 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:
- The years passed;
- Leap years passed;
- The month where we are;
- The day when we are;
- The hour when we are;
- The minutes where we are;
- 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).
- 'years since 1900' âˆ™ 'seconds in a non-leap year' = (y - 1900) âˆ™ 31536000
- '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.
- 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;
- 'days elapsed since the first day of the current month' âˆ™ 'seconds in a day'= (d - 1) âˆ™ 86400
- 'hours passed from the beginning of the day' âˆ™ 'seconds in an hour' = 'current hours' âˆ™ 'seconds in an hour' = 3600 h
- 'minutes passed from the beginning of the hour' âˆ™ 'seconds in a minute' = 'current minutes' âˆ™ 'seconds in a minute' = 60 i
- '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:
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:
The same applies to the date of birth D / M / Y - H : I : S (G and 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:
To get the minutes, hours, days of age just divide the seconds of age, respectively: 60, 3600 e 86400.