How can i calculate day number from a unix-timestamp, in a mathematical way and without using any functions and in simple math formula.
1313905026 --> 8 (Today 08/21
There is no simple formula to do this. You would need to subtract the number of years (accounting for leap years) since the epoch, which would probably require a loop or a discrete calculation of some kind. Then use some type of loop to subtract out the number of seconds in each month for the current year. What you are left with is the number of seconds currently into the month.
I would do something like this.
x = ...//the number of seconds
year = 1970
while (x > /*one year*/){
x = x - /*seconds in january, and march-december*/
if(year % 4 == 0){
x -= /*leapeay seconds in february*/
}else{
x -= /*regular seconds in february*/
}
}
//Then something like this:
if(x > /*seconds in january*/){
x -= /*seconds in january*/
}
if(x > /*seconds in february*/){
x -= /*seconds in january*/
}
.
.
.
//After that just get the number of days from x seconds and you're set.
Edit
I recommend using date functions for simplicity, but here is a possible non-loopy alternative answer in case anyone needs it, or would care to develop it further.
First let t be the current time in seconds since the epoch.
Let F be the number of seconds in four years. That is three regular years and one leap year. That should be: 126230400.
Now if you take away all of the time contributed by F, you will get a remainder: y.
So y = n % F.
There are several cases now: 1. y is less that one year 2. y is less than two years 3. y is less than three years and less than two months 4. y is less than three years and greater than two months 5. y is less than four years
Note that 1972 was a leap year, so if you count up by four from 1970, wherever you left off will be a leap year in two years.
let jan, feb, febLY, mar, may, ..., dec be the number of seconds in each month (you'd need to calculate it out).
d represents the day number of the current month and D represents the number of seconds in a day (86400). y represents the number of seconds in a regular year, and yLY represents the number of seconds in a leap year.
y = (t % F)
if(y < Y){
if(y > jan){
y -= jan
}
if(y > feb){
y -= feb
}
.
.
.
d = y % D
}
else if(y < 2 * y){
y = y - Y
if(y > jan){
y -= jan
}
if(y > feb){
y -= feb
}
.
.
.
d = y % D
}
else if(y < 2 * y + yLY){
y = y - 2 * Y
if(y > jan){
y -= jan
}
if(y > febLY){
y -= febLY
}
.
.
.
d = y % D
}
else{
y = y - 2 * Y - yLY
if(y > jan){
y -= jan
}
if(y > feb){
y -= feb
}
.
.
.
d = y % D
}
Not tested. Also, since the Earth doesn't spin at EXACTLY 1 rotation / 24 hours, they've occasionally made adjustments to time. You need to do a bit of research factor that in.