Show different content based on date

10 posts by 4 authors in: Forums > CMS Builder
Last Post: August 2, 2016   (RSS)

Hi,
I have a client running a membership site that offers a different set of plans every month.

On the member dashboard I would like to be able to show that we are in Month number XX and Week 1, 2, 3 or 4 of that month. I'd like this to update automatically each month and week.

When it is week 1, I would like to show text below this notice. When it is month 2, 3 or 4 I would like to show an alternate set of text.

NB - The dates do not follow a regular calendar month but are fixed, so I could set a beginning and end date.

Is this something that could be easily achieved in CMSB somehow or would I be better off finding javscript somewhere to achieve this?

By gregThomas - June 23, 2016

Hey superhappybunnycat,

I think it will be easier to build this functionality in PHP than JavaScript, although it would be possible to create it in both languages.

PHP doesn't have any date functionality to work out the week number by default. But I found this stackoverflow post that has a function to work out the week number for the current month:

http://stackoverflow.com/questions/5853380/php-get-number-of-week-for-month

You can return the current month number using this function:

<?php echo date('m'); ?>

You could check if we're in week 1 of the current month by combining the function in the stackoverflow post above with this if statement:

<?php if(getWeeks(date('Y-m-d'), "sunday") == '1'): ?>
  <p>Custom Message</p>
<?php endif; ?>

NB - The dates do not follow a regular calendar month but are fixed, so I could set a beginning and end date.

Does this mean you'd like to calculate the number of months and weeks since a particular date instead of from the beginning of the year?

Thanks!

Greg

Greg Thomas







PHP Programmer - interactivetools.com

Does this mean you'd like to calculate the number of months and weeks since a particular date instead of from the beginning of the year?

Yes I think that's what I need to do.

Each "Month" of the program goes for exactly 4 weeks and then it's a new month so it doesn't follow calendar months. Currently a new month starts about mid month. As an example it's currently Month 5 of the program which runs from Mon 13th of June, through to Sunday 10th of July.

So when it ticked over to Month 6 on Monday July 11th I'd want it to change the month to Month 6, and also reset to week 1, and then be able to count through week 2, week 3 and week 4 and then get to Month 7 and so on.

By gregThomas - June 24, 2016

In that case you probably want to manually calculate the time between the two dates manually:

<?php

  //number of months
  $now        = time(); // or your date as well
  $your_date  = strtotime("2010-01-01");
  $datediff   = $now - $your_date;
  $months     = ($datediff/(60 * 60 * 24 * 7 * 4));
  
  
  //number of weeks
  $now        = time(); // or your date as well
  $your_date  = strtotime("2010-01-01");
  $datediff   = $now - $your_date;
  $weeks      = floor($datediff/(60 * 60 * 24 * 7));
  $monthWeeks = $months * 4;
  $weeks      = $weeks - $monthWeeks;

So in the code example above you'd replace 2010-01-01 with your start date. Then it calculates the amount of time in months that have passed.

Next it calculates the number of weeks that have passed, it converts the number of months that have passed into weeks and subtracts that from the total weeks. 

This is just example code, so you might have to make a few changes to get it working. It's based on the code in this stackoverflow post:

http://stackoverflow.com/questions/2040560/finding-the-number-of-days-between-two-dates

Cheers,

Greg

Greg Thomas







PHP Programmer - interactivetools.com

By Daryl - July 11, 2016

Hi superhappybunnycat,

I tweaked Greg's code a bit to return the correct week value:

  //number of months
  $now        = time(); // or your date as well
  $your_date  = strtotime("2016-05-15");
  $datediff   = $now - $your_date;
  $months     = floor($datediff/(60 * 60 * 24 * 7 * 4));
  
  
  //number of weeks
  $now        = time(); // or your date as well
  $your_date  = strtotime("2016-06-15");
  $datediff   = $now - $your_date;
  $weeks      = floor($datediff/(60 * 60 * 24 * 7));
  $monthWeeks = $months * 4;
  $weeks       = $monthWeeks - $weeks;

And yes, you'll use $weeks.

However, this doesn't reset the week number every 4 weeks and doesn't increment the month day.

So I came up with a new solution:

$monthNumber          = 5;
$monthStartDate       = '2016-06-13';
$currentStartWeekDate = date('l') != 'Monday' ? date("Y-m-d", strtotime("last monday")) : date("Y-m-d"); // get the current week's Monday's date

$weekDateCounter      = $monthStartDate;
$currentWeekNumber    = 0;

while ($weekDateCounter != $currentStartWeekDate){
  $currentWeekNumber += 1;
  $weekDateCounter    = date("Y-m-d", strtotime($weekDateCounter . "+7 days"));
  
  //
  if ($currentWeekNumber == 4){
    $currentWeekNumber  = 0; // reset week number
    $monthNumber       += 1; // increment month number
  }
}

In the code above, since your program doesn't follow the calendar months, we need to set the start date of the program: $monthStartDate = '2016-06-13';
which is in the month 5: $monthNumber = 5;

The script will loop from the $monthStartDate, adds + 1 to week number, until we've reached the current week's Monday's date.
And every time the week number becomes to 4, we'll reset it and add + 1 to month number.

You can check the current week number using  $currentWeekNumber
and the current month number to $monthNumber;

Please let me know any questions.

Thanks,

Daryl Maximo
PHP Programmer - interactivetools.com

This is so close but it's still not working properly just yet.

Here's a list of what should be displayed based on some test dates and what is actually displayed:

Jun-20: Should be Week 2 - Shows as Week 0
Jun-27: Should be Week 1 - Shows as Week 3
Jul-04: Should be Week 4 - Shows as Week 2
Jul-11: Should be Week 3 - Shows as Week 1
Jul-18: Should be Week 2 - Shows as Week 0


I updated the code to try and fix it and managed to get Week 4 to show occasionally but it's still not correct :(

The updated code below produces:
Jun-20: Should be Week 2 - Shows as Week 0
Jun-27: Should be Week 1 - Shows as Week 4
Jul-04: Should be Week 4 - Shows as Week 3
Jul-11: Should be Week 3 - Shows as Week 2
Jul-18: Should be Week 2 - Shows as Week 1

$monthNumber          = 5;
$monthStartDate       = '2016-07-18';
$currentStartWeekDate = date('l') != 'Monday' ? date("Y-m-d", strtotime("last monday")) : date("Y-m-d"); // get the current week's Monday's date

$weekDateCounter      = $monthStartDate;
$currentWeekNumber    = 1;

while ($weekDateCounter != $currentStartWeekDate){
  $currentWeekNumber += 1;
  $weekDateCounter    = date("Y-m-d", strtotime($weekDateCounter . "+7 days"));
  
  //
  if ($currentWeekNumber == 5){
    $currentWeekNumber  = 0; // reset week number
    $monthNumber       += 1; // increment month number
  }
}

By Damon - July 25, 2016

Did you change this code:

$monthNumber          = 5;

to this

$monthNumber          = 7;

Cheers,
Damon Edis - interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/

Oh so does that mean I will manually need to keep entering the number of the calendar month for it to work? Since the weeks do not run off calendar months I'm confused about this part

For anyone else looking to do something similar I ended up using a different method below, which is working perfectly

function weekCounter($startDate,$endDate=null){

    //use today as endDate if no date was supplied
    $endDate = $endDate? : date('Y-m-d');

    //calculate # of full weeks between dates
    $secsPerWeek = 60 * 60 * 24 * 7;
    $fullWeeks = 
            floor((strtotime($endDate) - strtotime($startDate))/$secsPerWeek);

    $fullMonths = floor($fullWeeks/4);
    $weeksRemainder = $fullWeeks % 4; // weeks that don't fit in a month

    //increment from 0-base to 1-base, so first week is Week 1. Same with months
    $fullMonths++; $weeksRemainder++;

    //return months and weeks in an array
    return [$fullMonths,$weeksRemainder];
}



//list() will assign the array members from weekCounter to the vars in list
list($months,$weeks) = weekCounter('2016-06-07'); //no end date, so today is used

//now $months and $weeks can be used as you wish
echo "Month: $months, Week: $weeks"; //outputs Month: 2, Week: 2