How can I calculate the weekno. based on a date field?
Hi Wilfred,
Well, a date is represented by the number of seconds between the specified date and January 1, 1970 00:00:00. So you would have to subtract the current date from the start of the year to find out how many seconds have passed since the beginning of the year. So perhaps something like this:
($now – 1420070400) / 60 / 60 / 24 / 7
Where 1420070400 represents the Unix epoch date of January 1, 2015 00:00:00 GMT. Divide by 60 to turn into minutes, then by 60 again to turn into hours, then by 24 to turn into days, then by 7 to turn into weeks. So you would get the number of 7 day periods since the beginning of the year.
Thanks,
Brendan
To follow this up, it’s a bit more complex if you want the week number according to the ISO definition (Wherein Week 1 is the week containing 4 days of the new year i.e. containing Jan 4th)
So you have the get the date of the Monday of the week in which Jan 4 this year lies, then calculate the current week from that, using basically the formula above.