How to calculate the number of days between a date inputted by the user and the current date

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.

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

Similar to the above approach you can also calculate the number of years between a certain date inputted by the user and today’s date. The expression for this is:
{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.

Limesurvey – Conditions based on token attributes

Sometimes you want to show certain questions of a survey to a specific user group only. With Limesurvey this is no problem. If your survey is set to be non-anonymous and uses tokens, you can use user defined attributes for conditions. Here we describe the necessary steps.

1. Adding additional attributes

Set the survey to be non-anonymous:
Limesurvey survey settings

Enable tokens:
Enable tokens at Limesurvey

Add a certain number of user defined attribute fields:
Add token attributes at Limesurvey

Assign attribute names after having created the new fields:
Label additional attributes

Add some dummy tokens with user defined values

Add dummy tokens

Set conditions for a certain question

Go to the question and call the condition designer:
Limesurvey condition editor

Select the token field on which the condition should be based on (e. g. “Department”) and define the constant which the condition should match (e. g. “Management”):
Condition based on token attribute

Check condition

You can use the group preview for a first check if the condition was stored successfully,
Limesurvey group preview

because in this case the question will not be shown at the group preview (because the conditions isn’t met). The arrow indicates where the question was previously shown before setting the condition:

Test survey at runtime

Once tokens were assigned when adding users or by using the “Generate tokens” feature, the “Display tokens” screen contains an icon to test each token entry:
Limesurvey token overview

Of the above users, only “Jane” belongs to the department “Management”. Let’s check the different survey behavior to check if the condition works as expected:

Survbey at runtime Survey at runtime

More information

 

How to randomly show 1 of X questions at a Limesurvey survey

Sometimes you want to show different versions of a question at a Limesurvey survey. To do so, you need to randomly show one of the X question versions to the user so the first step would be to generate a random number between 1-X. For this we can use the powerful Limesurvey Expression Manager which is available since release 1.92.

The new question type “equation” can be used to store the result of the random function. So create an “equation” question with question code “randnumber” and the following content at the question text field, which will generate a random number between 1 and 2:
{if(is_empty(randnumber.NAOK),rand(1,2),randnumber.NAOK)}

The whole code basically means that the system should first check if the rand variable was already populated. If it is still empty, a random number between 1 and 2 is created. Else, the already created number is kept and no new random number gets created.

If you want to hide the question from the user, set “Always hide this question” to “yes” at the advanced question settings.
Now we have a question that will be filled with either “1” or “2”, we can use conditions at following questions to show version A or version B of a question. For this you can use the Limesurvey condition manager and set a condition based on a constant value of 1 or 2 for the equation question. Alternatively you can use the “relevance” field to define when a question should be shown. The appropriate relevance reads:
((randnumber.NAOK==”1″))

…and for the second question:
((randnumber.NAOK==”2″))

Don’t forget to check if you set up everything correctly by having a look at the survey logic file.

…and if this still sounds too tricky, we can set up the survey for you.

Pre-filling a text field at Limesurvey 1.92 using default answers and Expression Manager

At Limesurvey 1.92 we implemented default values for several question types that were missing this feature. In combination with the new and powerful Expression Manager, this offers new possibilities. Using the Expression Manager at Limesurvey 1.92 you can now easily populate a text field with data from previous answers.

Let’s create a simple example for demonstrating how it works:

1. Create a text question at Limesurvey and note the question code:
Limesurvey_text_question-question_code
2. Create a second question of the same type and e.g. ask for the email or last name. Again, note the question code

3. At a third text question, where you want to pre-populate the text field with the previously entered texts, click the “Edit default answers for this question” icon:
Limesurvey edit default answers icon

 

 

4. At the field to enter a default value, add the question codes in {…} brackets:
Limesurvey_default_answers

 

 

5. If you then fill in text for the first two questions, the answers should be inserted at the question for which you defined the default value.

 

Things to watch out for:

  • The question for which you set the default answer has to be shown on a following page.
  • When setting a default value use {…} brackets when referring to a question code of a previous question.

More Information:

 

How to conditionally hide/show groups in Limesurvey

When first working with Limesurvey’s condition designer, you might miss a feature to show/hide whole question groups. Doing this is quite obvious though:

  1. Set a condition on the first question of the group like “Show question X if question Y was answered ‘>=3’ “.
  2. Click the “copy conditions” icon at the condition editor.
  3. Select the previously set condition on top and at the list of following questions select all questions of the according group:

    Show/Hide groups at Limesurvey by using conditions
    Show/Hide groups at Limesurvey by using conditions
  4. Click the “copy conditions” button
  5. Done 🙂

Note: At Limesurvey 1.92 you can use the Expression Manager for hiding groups.