# How to count the time until the next day of the week?

I wants to do in Arduino countdown that shows how many days, hours and minutes remaining to Friday 12:00. In my program I used a function in php `strtotime('next friday')` which thus could calculate differential timestamp and show how much time is left until Friday 12:00.

Is there a function `strtotime('next friday')` in Arduino? I have RTC which gives me just the current date and time, but does not show the current day of the week.

EDIT: I have a solution

We have day of week. We multiplied by 86400.
We have an hour. We multiply by 3600.
We have a minute. We multiplied by 60.
Subtract this from 604800.

`nextFriday = 604800-((dayofweek * 86400)+(now.hour()*3600)+(now.minute()*60)+now.second());`

Important, Friday is the number of the week "0". Friday = 0; Saturday = 1; etc...

• Mosts RTCs have a dayOfWeek field. – Gerben Sep 18 '15 at 14:00
• In C/C++ it is `strtotime("next friday")` - note the double quotes. Single quotes do something quite different. – Nick Gammon Sep 18 '15 at 21:20

The key to working with time is to have a known "base" time to calculate everything relative to. This is called the epoch. Different systems use different epochs, and each has advantages and disadvantages.

I generally use the UNIX epoch in my programming (chiefly because I am a UNIX nut), which is the first of January, 1970 (01/01/1970). This is year, month, day, hour, minute and second 0.

From that you can then calculate the number of seconds between the epoch and any other time you choose as long as it is later than the epoch.

To calculate the day of the week for any given date you first need to know the day of the week for the epoch (for UNIX that's a Thursday) then work out how many days there are between the epoch and the target time.

Calculating real times in terms of seconds can be somewhat tricky due to leap years and daylight savings, but there are plenty of algorithms in the public domain, and many RTC libraries include functions to convert to/from epoch times.

For instance, the Arduino Time Library has functions to calculate weekdays, and to convert to "time_t" format (seconds since epoch) which can be used to work out differences in times. For instance, if you know that it's Tuesday and you want next Friday then you know that Friday is 3 days in the future - so you can get the current time in seconds from the epoch then add to that 3 days' worth of seconds (3 × 60 × 60 × 24) to that time to get the same time of day next Friday.

There are generic examples, e.g. Determination of the day of the week - Wikipedia.

In the C language expressions below, y, m and d are, respectively, integer variables representing the year (e.g., 1988), month (1-12) and day of the month (1-31).

``````        (d+=m<3?y--:y-2,23*m/9+d+4+y/4-y/100+y/400)%7
``````