Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determine if a week is odd or even

Tags:

php

datetime

I have debugged this legacy code, and would like a sanity check on it.

The purpose of it is to allow someone to choose a delivery frequency for shipping a product. If someone wants their product Every Other Week, the system needs to determine if they should get an order next week, or two weeks from now. We call it A week, or B Week.

Keep in mind I did not write this, I am just trying to make sense of it and would like some help evaluating its accuracy:

if (date("l") == "Monday" ) {
            $start = 0;
        } else if (date("l") == "Tuesday" || date("l") == "Wednesday" || date("l") == "Thursday" || date("l") == "Friday" || date("l") == "Saturday"|| date("l") == "Sunday") {
        $start = -1;
    }

    // if changing to every other week set to next week's a/b-ness
    $a_week_tid = 34;
    $b_week_tid = 35;

    $every_other_week_frequency_id = 32;

    if ($delivery_frequency == $every_other_week_frequency_id) {
        $julian = (int) (strtotime('Monday +' . $start . ' week') / 86400);
        $julian_week = ($julian-4) / 7;
        if ($julian_week % 2) {
            $today_a_or_b = $b_week_tid;
            $next_week_a_or_b = $a_week_tid;
            $a_or_b_week_string = '(A Week)';
        } else {
            $today_a_or_b = $a_week_tid;
            $next_week_a_or_b = $b_week_tid;
            $a_or_b_week_string = '(B Week)';
        }
    } else {
        $next_week_a_or_b = NULL;
        $a_or_b_week_string = NULL;
    }

This code is not commented or documented. The part that confuses me is:

  1. Why is 4 subtracted from Julian, then divided by 7?
  2. If today is Monday, $julian_week is 2129, and 2129 % 2 evaluates TRUE. Is that correct?
  3. Is this even how it should be done? Can't I rewrite this using date('w') a lot easier?
like image 875
Kevin Avatar asked Oct 19 '10 15:10

Kevin


People also ask

How do I know if its an odd or even week?

How can I check if a given week is odd or even? The "What week number is it now" Calculator returns a number if it is even, then the week is even, if it is odd, then the week is odd. (Even numbers are always ending in 0, 2, 4, 6, 8, and odd numbers ending in 1, 3, 5, 7, 9 ends).

What days of the week are odd?

There are seven days in a week such as Monday, Tuesday, Wednesday, Thursday, Friday, Saturday and Sunday. Among these, Monday, Wednesday, Friday and Sunday can be identified as the odd days and Tuesday, Thursday and Saturday can be considered as the even days with respect to a week.

What does even week mean?

on even weeks. means on the second, forth, sixth, eighth, etc week of the year. In your usage "on" = "during", since during those weeks you do something, or "at that appointed time". There is bus service on even weeks. there is bus service every other week.

What week is this week?

Week 42. Week 42 is from Monday, October 17, 2022 until (and including) Sunday, October 23, 2022.


2 Answers

Yeah using date would totally be easier, plus it takes into account leap years, daylight saving time, all that extra stuff you don't want to have to deal with.

if (date('W')%2==1)

That's SOOOO much easier to maintain than the above.

like image 113
Bob Baddeley Avatar answered Sep 24 '22 07:09

Bob Baddeley


I don't believe you can use date("W") in this case. According to the ISO calculation, on occasion, there will be years with 53 weeks. In those years, Week 53 is followed by Week 01, both odd numbers, and an A/B calculation based on Even/Odd ISO week number would result in two successive A or B weeks.

The original calculation determines the number of days from the UNIX epoch of the present Monday, or of the most recent Monday if today is not a Monday. The -4 causes the A/B week labels to change on Thursdays. Even/oddness of a week is determined from a fixed date (the Unix Epoch), so there will be no discontinuity in the oscillation of A/B-ness using the original code.

like image 24
Bret Whissel Avatar answered Sep 23 '22 07:09

Bret Whissel