[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

correct syntax of mod operation

Posted on 2013-05-28
5
Medium Priority
?
238 Views
Last Modified: 2013-06-03
PHP
4 different week(date) types
this week is the 21st week of the year: this could be week_type 1
next week: week_type: 2
following week: week_type: 3
final week: week_type:4
week after: week_type:1


what is correct syntax of mod operation in this instance
0
Comment
Question by:rgb192
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 1400 total points
ID: 39203112
This is actually the 22nd week of the year. Where do you want to go from here?

<?php // RAY_temp_rgb192.php
error_reporting(E_ALL);

// See: http://php.net/manual/en/function.date.php
// See: http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_28141205.html

$week_number = date('W');
var_dump($week_number);

Open in new window

0
 
LVL 82

Assisted Solution

by:hielo
hielo earned 600 total points
ID: 39203234
>>this could be week_type 1
Instead of week_types from 1...4, use week_types from 0...3.  Then the mod operation you need is:
$week_type = $week_type % 4;

If you really want to use the values 1...4, then you will need to add 1 to the result mod operation and then $week_type will always be a value between 1 and 4 (inclusive):
$week_type = ($week_type % 4) +1;
0
 

Author Comment

by:rgb192
ID: 39203544
This is actually the 22nd week of the year. Where do you want to go from here?
want week type of month not year


by: hieloPosted on 2013-05-28 at 20:33:36ID: 39203234
$week_type = $week_type % 4;

how do I instantiate variable $week_type
0
 
LVL 111

Accepted Solution

by:
Ray Paseur earned 1400 total points
ID: 39204214
want week type of month not year
Perhaps the reason that this question is confusing goes to the number of days in a week and the number of weeks in a month.

There are 7 days in a week.  There are more than 28 days in a month.  Yet the question as written seems to assume that some modulus will give a sensible answer to the week number and the resulting week numbers will be 1,2,3,4.  To me that is illogical.  What will happen is that the modulus will cause days 1-7 to be recognized as week 0 (then you can add one) and will cause days 21-28 to be recognized as week 3 (again, you can add one).  Days 29-31, if present in the month, will be misunderstood (at least to my way of thinking).

What week number will you want to apply for today (May 29, 2013)?  Once we understand that we may be able to help you with a more consistent application of program logic.

Try installing this and running it to see what I'm talking about.  Then lets' see if we can clarify the question.
<?php // RAY_temp_rgb192.php
error_reporting(E_ALL);
echo '<pre>';

// THE DAYS OF THE MONTH
$days = range(1,31);

// COMPUTE A WEEK NUMBER FOR EACH DAY
foreach ($days as $day)
{
    $wom = (int)(($day-1) / 7) + 1;
    echo PHP_EOL . "DAY $day IS PART OF WEEK: $wom";
}

Open in new window

Thanks and regards, ~Ray
0
 

Author Closing Comment

by:rgb192
ID: 39217976
the code without mod was easier to follow
thanks both
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.
Suggested Courses

650 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question