Jump to content

Recommended Posts

Hi Guys,

 

I'm new to PHP. I've gone through a few tutorials and I'm not sure how to go about the following:

 

For work, every week 1 of 3 people is responsible for checking some bug report. This week I have to do this.

Next week it's Tom the week after that is Brenda. Then it's back to me again and it cycles. We currently have a white board calendar at the back of the office that gets updated with the name of the person responsible for doing the checks that week.

 

What I'm trying to do is write a small PHP snippet that will show on our internal site something like:

 

Bug Checker this week is: TARA

Then the next week it'll show Tom, then Brenda. And cycle back. That way we don't have to keep updating that board. Does anybody know the best way to go about this. I keep thinking of different ideas but they all seem pretty convoluted and I'm fairly certain there's got to be a very easy way to do it; it's just not coming to me.

 

 

Any help would be appreciated  Thanks so much!

 

XXX

Tara

 

EDIT: We cycle through every Monday. If that helps.

 

I considered creating a database file with the date and the person and just read that in but that is so embarassingly inefficient that I didn't even want to mention this in the post.

Link to comment
https://forums.phpfreaks.com/topic/211646-output-1-of-3-names-every-week/
Share on other sites

Can be easily done, if you'd want I can write up a small PHP application for you. Just a question, is it the same day every month? I'd assume so.

 

I would love that! lol, but with only 1 post on these forums so far I would feel out of line asking for that :P

 

I'm not sure what you mean by it being the same day every month. It basically cycles right through the month so 1st week of august it's Tara, 2nd Tom, 3rd Brenda, 4th Tara then first week of September it would be Tom. I hope I answered your questions/made sense.

 

You can achieve this with some simple math. Something like this should work:

 

$arr = array('Branda', 'Tara', 'Tom');
echo 'Bug checker this week: ' . $arr[(date('W') == 1) ? 2 : date('W') % sizeof($arr)];

 

I'm heading home right now but I'm going to try to wrap my head around this tomorrow at work, thanks so much! I hope I don't turn out to be impossibly dense.

I've taken AlexWD's example and modified it to give you a list of the Weeks and Bug checkers for X number of weeks. I considered having it generate results for the entire year, but if you ever needed to add/remove a person the previous dates would not be correct. If you do ever remove/add a person just adjust the array positions as needed.

 

The function takes 1 optional parameter for the number of week you want to see the assignments (starting from the current week). If the parameter is missing it only returns the current week's bug checker. The function returns an array with the first day of the week as the key and the staff person as the value. You can use it to output the day however you wish.

 

function getBugCheckers($weeks=1)
{
    //Array of staff
    $staff = array('Branda', 'Tara', 'Tom');
    $bugCheckers = array();
    //Get offset for monday of this week
    $offset = -1*(date('N')-1);
    for($week=0; $week<$weeks; $week++)
    {
        //Get total offset for the week iteration
        $startDayIdx = $offset + (7*$week);
        //Get timestamp for the Monday
        $startDayTS = strtotime("{$startDayIdx} day");
        //Determine the responsible staff person
        $bugCheckers[date('F j, Y', $startDayTS)] = $staff[date('W', $startDayTS)%count($staff)];
    }
    return $bugCheckers;
}

//Usage
$responsibleStaff = getBugCheckers(7);
print_r($responsibleStaff);

 

Output:

Array
(
    [August 23, 2010] => Tara
    [August 30, 2010] => Tom
    [september 6, 2010] => Branda
    [september 13, 2010] => Tara
    [september 20, 2010] => Tom
    [september 27, 2010] => Branda
    [October 4, 2010] => Tara
)

mjdamato, there's a small problem with your code. I ran into the same problem which is why I have this:

 

(date('W') == 1) ? 2 : date('W')

So that if it's the first week of the month it uses 2 instead of 1. Otherwise you run into a problem where Tara is assigned for the last week and first week of the year. This only happens because 52 % 3 is the same as 1 % 3. Example output in your script from that point:

 

[December 29, 2010] => Tara
[January 5, 2011] => Tara

Otherwise you run into a problem where Tara is assigned for the last week and first week of the year.

 

Right, I did that on purpose knowing that Tara is thier most competent person. I deduced that the company would liek to end the year on a high note and start the year off running.

 

jk. thanks for the catch.

Otherwise you run into a problem where Tara is assigned for the last week and first week of the year.

 

Right, I did that on purpose knowing that Tara is thier most competent person. I deduced that the company would liek to end the year on a high note and start the year off running.

Ha, good one :P

<?php
//echo date('D', strtotime('71/1/1'));
$staff = array('Branda', 'Tara', 'Tom');
echo $staff[((time()+3*24*3600)/(7*24*3600)) % 3];
?>

small update

 

echo $staff[((time()+3*24*3600)/(7*24*3600)) % count($staff)];

Another small update to sasa's solution: The array will need to be ordered like this:

 

$staff = array('Tara', 'Tom', 'Branda');

Well guys, I can't express my gratitute for your help. This is awesome and looks great on the site!

 

One problem though, I'm somewhat depressed at how staggeringly good you guys are at this. Please tell me you guys code PHP daily and have been doing it for a decade. Please lol

Actually I have a question - just trying to understand this.

 

echo $staff[((time()+3*24*3600)/(7*24*3600)) % count($staff)];

 

so this returns the array value (1,2 or 3) for staff thus returning/echo the appropriate name.

 

I guess my question lies in this part lol: ((time()+3*24*3600)/(7*24*3600)) % count($staff)

 

What I can see is it's something divided by number of seconds in a week mod 3 (or however many elements are in the array). What do the components of (time()+3*24*3600) mean?

 

 

And how does it work overall, I'm sorry I'm not getting this, I just really want to get better at this sort of thing. It's amazing what can be done!

I guess my question lies in this part lol: ((time()+3*24*3600)/(7*24*3600)) % count($staff)

 

7 * 24 * 60 is: 7 days, 24 hours, 60 minutes.

 

It rotates the text it holds in the array every [time] (depends on what it set) it would need to be.

I guess my question lies in this part lol: ((time()+3*24*3600)/(7*24*3600)) % count($staff)

 

7 * 24 * 60 is: 7 days, 24 hours, 60 minutes.

 

It rotates the text it holds in the array every [time] (depends on what it set) it would need to be.

 

So every 7 days. What does (time()+3*24*3600) refer to? Is this in reference to the "starting position" so to speak. Because what's to stop it from giving the first name in the array every time it's loaded up, it must have a starting point against which it is referencing the current week/date. I'm assyming that's what this snippet of code does. Am I wrong?

 

24 is number of hours in a day

3600 is number of seconds in an hour

what is the 3 there for?

time() is the current time correct?

so current time * 3 * 24 * 3600...

Good question Tara, a reference point is indeed required otherwise indeed someone can end up working harder than someone else. Here is a starter, tomorrow ill post more, been up a little too long. But here is a short answer: time() is the reference point. At the reference manual at php.net they say:

Time() Returns the current time measured in the number of seconds since the Unix Epoch (January 1 1970 00:00:00 GMT).

 

Since I have to check out this code better myself ill try to share my findings tomorrow. Thing that really keeps my mind goggling is the 3, maybe it should have been count($staff). Although the result will be the same for future activities it would be nice to know indeed why its 3 and not count($staff).  :confused:

making the code :

echo $staff[((time()+count($staff)*24*3600)/(7*24*3600)) % count($staff)];

Ill find that out tomorrow. ::)

time return number of seconds from jan 1st 1971 to now

jan 1st 1971 is thusday and i add 3 days (3*7*24*3600) to move to monday as start  ofr week

Awesome your a genius ty! lol, i was allready faking up my windows  ::)calender to get to january first 1971.

Thx!

Well guys, I can't express my gratitute for your help. This is awesome and looks great on the site!

 

One problem though, I'm somewhat depressed at how staggeringly good you guys are at this. Please tell me you guys code PHP daily and have been doing it for a decade. Please lol

The more time you spend coding the more concepts you become exposed to for example here sasa came up with a great concept of converting the time to a number of the week, but then needed it to start from Monday,

 

Breaking his code down may make it easier to understand

<?php
$staff = array('Tara', 'Tom', 'Branda');

// time() starts from Thursday (jan 1st 1971) so add 3 days
$start_of_week = time()+(3*24*60*60) + (7*24*60*60);

//Breaks date into weeks (days of 7)
$week_into_days = $start_of_week/(7*24*60*60);

/* Now we divide $week_into_days by 3 and get the remainder,
   So, day 1=1, 2=2, 3=3, 4=1, 5=2 */
echo $staff[$week_into_days % count($staff)]; //day Mod 3,

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.