So you somewhere have a continuous Date field or calendar the user makes his selection in, right?
You can use INTERVALMATCH LOAD prefix in your script to link your flag periods to this date field (finally, I would probably join the flag to your calenda).
Then, doing your calculation is just summing the Flag value while user selects a Date field period.