Welcome to the LimeSurvey Community Forum

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

SOLVED: Calculate a date 5 day in the future

  • blocka
  • blocka's Avatar Topic Author
  • Offline
  • Platinum Member
  • Platinum Member
More
3 years 8 months ago #201995 by blocka
Calculate a date 5 day in the future was created by blocka
I need to save a date that is the is a sum of the current date plus 5 days. This will allow me to display a deadline to the participant like:

Today's day is 2020-06-29: You need to mail in your sample by 2020-07-04.

That is, I want to calculate today's date + 5 days.

Can anyone suggest how this can be done?
The topic has been locked.
  • blocka
  • blocka's Avatar Topic Author
  • Offline
  • Platinum Member
  • Platinum Member
More
3 years 8 months ago #201996 by blocka
Figured it out, for others:

{( date('d F Y',((strtotime(date('Y-m-d')) + (60 * 60 * 24 * 5) ))) )}

Where the 60 * 60 * 24 is number of seconds in a day, and 5 is the number of days.
The following user(s) said Thank You: holch
The topic has been locked.
  • DenisChenu
  • DenisChenu's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
3 years 8 months ago #202017 by DenisChenu
Replied by DenisChenu on topic SOLVED: Calculate a date 5 day in the future

Assistance on LimeSurvey forum and LimeSurvey core development are on my free time.
I'm not a LimeSurvey GmbH member, professional service on demand , plugin development .
I don't answer to private message.
The topic has been locked.
More
3 years 3 months ago #208880 by lindalimesurvey
Replied by lindalimesurvey on topic SOLVED: Calculate a date 5 day in the future
Is it also possible to do this calculation, not with a current date, but with a date the user has entered? So the user entered 01-01-2020 and I want to add x-days to this (the days are also a variable and not a fixed value in this case)

I would be really thankful for any help! :)
The topic has been locked.
  • Joffm
  • Joffm's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
3 years 3 months ago - 3 years 3 months ago #208886 by Joffm
Hi, you may use this equation:
Q1: the question where the date is entered.
Q1a: the question where the added days are entered

{date('Y-m-d',mktime(0,0,0,substr(Q1,5,2),sum(substr(Q1,8,2),Q1a),substr(Q1,0,4)))}

Explanation:
The date in Q1 has a format YYYY-MM-DD; this is the format the database stores a date.
mktime is a function to create a UNIX timestamp for a date and takes the parameters
mktime(hour , minute , second , month , day , year)
We neclect the time, but then we take the substring of the date from index 5 and 2 characters long (this is the month)
Then we take the substring of the date from index 8 and 2 characters long (this is the day)
At last we take the substring of the date from index 0 and 4 characters long (this is the year)

You also see the sum
{date('Y-m-d',mktime(0,0,0,substr(Q1,5,2),sum(substr(Q1,8,2),Q1a),substr(Q1,0,4)))}
Here we add the value of question Q1a to the day parameter.

{date('Y-m-d',mktime(0,0,0,substr(Q1,5,2),sum(substr(Q1,8,2),Q1a),substr(Q1,0,4)))}
And at last we recalculate to a date with the "date" function.
This function has two parameters, the date format and the timestamp.

All these functions are explained here
manual.limesurvey.org/ExpressionScript_-...mplemented_functions

Examples with two different date formats.
To do calculations you should always use "Y-m-d".



and



Joffm

Volunteers are not paid.
Not because they are worthless, but because they are priceless
Last edit: 3 years 3 months ago by Joffm.
The following user(s) said Thank You: lindalimesurvey
The topic has been locked.
  • DenisChenu
  • DenisChenu's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
3 years 3 months ago - 3 years 3 months ago #208892 by DenisChenu
Replied by DenisChenu on topic SOLVED: Calculate a date 5 day in the future
I check (in 3.25) : in a second page (another group)

The date + 5 days : {date('Y-m-d',strtotime(Date.NAOK+" +5 days"))}
is OK with dd/mm/yyy or dd.mm.yyy

Same page (same question or same group) it broke.

Assistance on LimeSurvey forum and LimeSurvey core development are on my free time.
I'm not a LimeSurvey GmbH member, professional service on demand , plugin development .
I don't answer to private message.
Last edit: 3 years 3 months ago by DenisChenu.
The following user(s) said Thank You: Joffm
The topic has been locked.
  • Joffm
  • Joffm's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
3 years 3 months ago #208895 by Joffm
And you may enter days dynamically

{date('Y-m-d',strtotime(Q1.NAOK+" +"+Q1a.NAOK+" days"))}

But - as Denis said - only on a next page.

Joffm

Volunteers are not paid.
Not because they are worthless, but because they are priceless
The topic has been locked.

Lime-years ahead

Online-surveys for every purse and purpose