Sometimes you want to find out how many days or years have passed between today’s date and a date inputted by the user at a Limesurvey survey. The process is a little complex but it can be done using the Limesurvey Expression Manager.

Contents

### Calculating the number of days

Let’s assume there is a question of type “date” at your survey which uses question code “q2”. The user can select any date using the date picker and at the help text you can show a message like “That was X days ago.”.

To calculate the value for X you can use this expression:

{floor((time() - strtotime(q2)) / (60 * 60 * 24))}

This looks a little complex, so let’s have a look at the details:

- To get the current time stamp we use
*time().* - Using the strtotime() Expression Manager function we create a time stamp based on the date inputted by the user at question “q2”:
*strtotime(q2)* - That time stamp gets subtracted from the current time:
*(time() – strtotime(q2))* - Since we are not interested in the time difference in seconds but want to know about the exact number of days we divide the result by
*(60 * 60 * 24)*because one day has 24 hours with 60 minutes and 60 seconds. - Finally, the result is rounded down using the
*floor()*function.

### Calculating the number of years

{floor((time() - strtotime(q2)) / (60 * 60 * 24 * 365))}

### Using calculated result within conditions/relevance equations

You can not only use this expression to output the results of the calculation within any (follow up) question but you can also use this code for conditions by entering it at the “relevance equation” field (without “{” and “}”). E.g. you can ask participants for their day of birth and then calculate if they are already 18 years old. If not, you can use conditions to hide certain follow up questions or show them a warning.

### Storing calculated result

If you want to store the calculated result all you need to do as adding the expression (with “{” and “}”) at an equation question. You can even hide that question (edit question -> advanced question settings -> “Always hide this question”) and the result will still be stored at the database.