Page 1 of 1

dates for given week

Posted: Thu Dec 14, 2006 10:01 am
by hillman
Is there an easy way in Vortex to calculate the first and last days of a given week (number). For example, if I know that the current week is week no. 50, how can I calculate that Monday is the 11th December and Friday is the 15th?

(The other way around is easy enough, but I can't figure how to arrive at a timestamp from just the week number...)

dates for given week

Posted: Thu Dec 14, 2006 11:03 am
by John
The easiest way is probably to use calrule to generate likely dates, e.g. something like

<strfmt %at "%Y-01-01" now>
<$startofyear=$ret>
<calrule RULE="every Monday" START=$startofyear SKIP=50 MAX=2>
<$Monday=$ret>
<$weekno=(week($Monday))>
<if $weekno eq 50>
<$Friday=($Monday + (4 * 86400))>
<break>
</if>
</calrule>

dates for given week

Posted: Wed Dec 20, 2006 5:27 am
by hillman
Thanks for your help John, but when I try that I get:

Invalid calendar rule syntax `every Monday' in the function vx_calrulenext

We're running version 5.1.11 so I think it should work?

dates for given week

Posted: Wed Dec 20, 2006 11:04 am
by hillman
I wrote my own function which does it the hard way!

(It might not be the most elegant method, but it works, so I'll post it incase anyone else is searching for this).

<!-- START OF CURRENT YEAR -->
<strfmt %at "%Y-01-01" now>
<$startyear = (convert($ret, 'date'))>

<!-- GET FIRST DAY AS NUMERIC -->
<strfmt %at "%w" $startyear>
<$firstday=$ret>

<!-- FIND THE FRIDAY PRECEEDING THE FIRST SUNDAY -->
<$offset = (-2 - $firstday)>

<!-- ADD THE CURRENT WEEK OFFSET -->
<strfmt %at "%U" now>
<$currentweek=(-1 + $ret)>
<$offset = ($offset + ($currentweek * 7))>

<!-- TIMES OFFSET BY NO OF SECONDS -->
<$secs = ($offset * 86400)>

<!-- RESULTS -->
<$lastfriday = ($startyear + $secs)>
<$nextthursday = ($lastfriday + (6 * 86400))>

(This version calculates the previous Friday and the following Thursday of the current week, simple because that's the date range we use, but could easily be ammended to do Mon-Fri or Mon-Sun etc)...

dates for given week

Posted: Wed Dec 20, 2006 12:01 pm
by mark
This will give the previous friday and it's following thursday.

<$Friday=(convert( '-friday' , 'date' ))>Friday=$Friday
<$Thursday=($Friday + (6 * 86400))>Thursday=$Thursday