boylesg:

All I did was calculate the year from 31556926 seconds.

Then I can get the number of seconds into this year and therefore the number of days into this year.

That will give you an answer, but it will be the wrong answer.

A calendar year is either 365 days or 366 days. In seconds, those figures would be 31536000 seconds and 31622400 seconds, respectively.

Your figure of 31556926 seconds equals 365 days, 5 hours, 48 minutes, and 46 seconds. I do not suppose you want the next second after 05:48:45 to be 00:00:00.

Also, I made a mistake. I gave you a function that converts in the direction opposite from what you want.

Here is a function I just dug up that converts a count of days since January 1, 2000 to a year, month, and day of the month. The main caveat is that it cannot handle dates before AD 2000.

```
uint32_t daysToYmd(uint16_t n) {
// Converts day number to date.
// Valid day numbers are 0 through 36524.
// 0 means 2000-01-01(Sat)
// 36524 means 2099-12-31(Thu)
if (n>36524) return 0xFFFFFFFF;
// Here we go!
n+=1401; // move starting point to 1996-03-01
uint8_t y=(n/1461)*4; // take care of 4-year intervals
n%=1461; // max 3 years 365 days (not 4 years!) remaining
if (n>=730) {n-=730; y+=2;} // 2 years
if (n>=365) {n-=365; y++;} // 1 year
uint8_t m=3; // get started on the month
// Note repeating pattern of month lengths:
// 31 30 31 30 31 31 30 31 30 31 31 <30
// Mr Ap My Je Jl Au Se Oc No De Ja Fe
if (n>=306) {n-=306; y++; m=1;} // for Jan. and Feb.
else if (n>=153) {n-=153; m=8;} // for Aug. thru Dec.
if (n>=122) {n-=122; m+=4;} // here, 122 days mean 4 months
else if (n>=61) {n-=61; m+=2;} // 61 days mean 2 months
if (n>=31) {n-=31; m++;} // 31 days means exactly 1 month
uint8_t d=n+1; // get the day of the month
y-=4; // make y be 0 for AD 2000
// Now we have the correct year, month, and day of the month.
// (Note: y is 0 for AD 2000, ... 99 for AD 2099)
// The following output is a "placeholder" for testing purposes.
// It should probably be replaced with something more useful.
return (((2000+y)*10000L)+(100L*m)+d);
}
```

From January 1, 1970 (the base date of the Unix epoch) to January 1, 2000 (the base date of my function) is 10957 days. That is 946684800 seconds. So, you can subtract 946684800 seconds from the Unix time you wish to convert, then use division and modulus to get days, hours, minutes, and seconds (the divisors being, in order, 86400, 3600, and 60), then feed the “days” number into my function to get the sought-after year, month, and day of the month.