Calculate weekno.

Tagged: ,

Viewing 2 reply threads
  • Author
    Posts
  • June 24, 2015 at 2:15 PM #14075

    Wilfred Hilbers
    Participant

    How can I calculate the weekno. based on a date field?

    June 25, 2015 at 3:36 PM #14085

    Brendan
    Keymaster

    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

    August 10, 2015 at 2:53 PM #14421

    Chris Ward
    Participant

    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.

Viewing 2 reply threads

You must be logged in to reply to this topic.