0

Trying to devise a way for PHP (or javascript) to change between two bits of text every Monday.

So on Monday it would say "Week 1" then following Monday say "Week 2" then back to "Week 1" the next week and so forth.

I have thought about loops etc but not getting the solution I need. Can anyone help?

Thanks

  • 1
    Did you try anything or were you already happy with that quick thinking of yours ? I would suggest taking inspiration from [this post](http://stackoverflow.com/questions/9567673/get-week-number-in-the-year-from-a-date-php) and base your week 1/2 on actual week number being odd or even... – Laurent S. Apr 28 '15 at 08:28

3 Answers3

3

Here is a very simple and effective way to do so :

The php function Date("W") (with big 'w') returns the number of weeks passed this year. Today is week 18 for example. you can use that to say week 1 if the number is EVEN and week 2 if the number is ODD

Example :

<?php

    $w   = date('W');
    $msg = ($w % 2 == 0) ? 'week 1' : 'week 2';

    echo $msg;
?>
Kristian Lilov
  • 610
  • 6
  • 12
0

Since a week starts on Monday, you can simply alternate by checking whether the current week number is a multiple of two.

if(date("W") % 2 == 0)
    echo 'Week 1';
else
    echo 'Week 2';
ksbg
  • 3,214
  • 1
  • 22
  • 35
0

I would use something like the following:

<?php
    $current_week = date('W');

    if ($current_week % 2 == 0) {
        echo "Week 1";
        // other actions / text
    } else {
        echo "Week 2";
        // other actions / text
    }
?>
Albrecht
  • 45
  • 1
  • 6