Search code examples
phpdatetimetable

PHP week1 and week2 system


Im trying to create a timetable in PHP. My timetable goes through a Week 1 and a Week 2. Week 1 comes straight after week 2 and week 2 comes straight after week 1, this repeats each week.

How can I use PHP to find which week I am in from a date?


Solution

  • W is the week number of year.

    function whatMyWeek($timestamp) {
        if ((int) date('W', $timestamp) % 2) {
            return 'week odd'; // Week 1 
        } else {
            return 'week even'; // Week 2
        }
    }
    
    // now
    echo whatMyWeek(time()); // week odd
    
    // a week ago
    echo whatMyWeek(time() - 60*60*24*7); // week even
    
    // two week ago
    echo whatMyWeek(time() - 60*60*24*7*2); // week odd