Welcome to the LimeSurvey Community Forum

Ask the community, share ideas, and connect with other LimeSurvey users!

Calculate age in months

More
9 months 3 weeks ago #273169 by Myriam123
Please help us help you and fill where relevant:
Your LimeSurvey version: Version 6.15.23+251110 
Own server or LimeSurvey hosting:
Survey theme/template:
==================
Hi,
I have a question asking for the date of birth in dd/mm/yyyy format. I would like to calculate the age in months from that question to then create conditions to only show certain following questions depending on their age in months.
Thank you 

Please Log in to join the conversation.

More
9 months 3 weeks ago #273171 by Joffm
Replied by Joffm on topic Calculate age in months
Hi,
what is your real concern?
a. How to calculate a difference generally or
b. how to calculate the difference in month?

a. This is rather easy.
The output of the date question is a string like 25/10/2023.
Now you use the function "strtotime(date)" to convert this string into a number, that outputs the number of seconds since 1/1/1970.

The function expects to be given a string containing an English date format and will try to parse that format into a Unix timestamp (the number of seconds since January 1 1970 00:00:00 UTC)

Now you need the actual date.
You do the same. Convert the actual date into this timestamp.
You may use
  • either  {strtotime('now')} (These verbatims you find in the manual: Question type "date")
  • or {strtotime(date('Y-m-d'))} (here the seconds since midnight are not taken into account)
To get the difference you subtract the two values and get the difference in seconds.
To get the difference in days you only have to divide by 86400.
{(strtotime('now')-strtotime(Q1))/86400}
Fine.

And now the reason why I asked about the difference in month.
Unfortunately there are months with 28 days and months with 31 days. So you can divide the number of days by 30.42 (what is the average length of a month in a regular year)
But this is up to you.
 

That there are no whole numbers of days is due to the switches from daylight saving time to standard time and viceversa.

Joffm
 

Volunteers are not paid.
Not because they are worthless, but because they are priceless
The following user(s) said Thank You: Myriam123

Please Log in to join the conversation.

More
1 week 1 day ago #275051 by Hamza32
Replied by Hamza32 on topic Calculate age in months

Hi, wrote:
 

This is a clear and practical explanation of the date calculation logic in Limesurvey. The approach Joffm outlined is solid, especially the note about daylight saving time affecting the day count and the average month length of 30.42 days.

For anyone implementing this in a production survey, I'd recommend using the {strtotime('now')} method for real-time calculations, as it automatically accounts for the server's current time. The manual date method with date('Y-m-d') can be useful when you need to test specific scenarios.

The conversion to months is indeed the trickiest part. Depending on your use case, you might want to round up or down based on whether you need the complete number of months or a more precise fractional value.
 

Please Log in to join the conversation.

Moderators: tpartnerholch

Lime-years ahead

Online-surveys for every purse and purpose