Jump to content

Recommended Posts

Alright - I spent most of the day wrestling with this one.

 

I have 6 shifts of people that rotate through our training throughout the year. I need to list them by week of the year, and the shifts will usually start the year NOT on 1. For example:

 

Week 1 - Week of Jan 04 = Shift 5

Week 2 - Week of Jan 11 = Shift 6

Week 3 - Week of Jan 18 = Shift 1

Week 4 - Week of Jan 25 = Shift 2

Week 5 - Week of Feb 01 = Shift 3

Week 6 - Week of Feb 08 = Shift 4

Week 7 - Week of Feb 16 = Shift 5

Week 8 - Week of Feb 22 = Shift 6

Week 9 - Week of Mar 01 = Shift 1

...etc

 

I was able to find the week of the year and the mondays, but I'm having a hard time matching the shifts with these dates.

 

<?php
$weekOfYear = "$year-$month-$day";
$weekOfYear = strftime("%W", strtotime("$weekOfYear"));
$weekOfYear = $weekOfYear * 1;
///////////

///FIND MONDAY
$todayIsMonday = "";
$findMonday = date('D', mktime(0,0,0,$month,$day,$year));
if($findMonday == "Mon") {
	$todayIsMonday = "<a href=\"events_shift.php?y=$year&m=$month&d=$day\">Shift $IneedToFindTheShiftNum</a><br />";
}
///FIND MONDAY
?>

Link to comment
https://forums.phpfreaks.com/topic/196664-help-with-calendar-weeks/
Share on other sites

Just set the three variables in the function:

 

The year, the shift that will do training on the first week, and the number of total shifts

<?php

function getShiftForWeek($weekNo)
{
    $output = array();
    $initialYear = 2010;
    $firstShift  = 5;
    $totalShifts = 6;

    $firstMonday = strtotime("first monday jan $initialYear");

    $output['week'] = $weekNo;
    $output['timestamp'] = $firstMonday + (($weekNo-1) * 604800);
    $output['monthDay']  = date('M d', $output['timestamp']);
    $output['shift'] = (($weekNo + $firstShift - 1) % $totalShifts);
    if ($output['shift']==0) { $output['shift'] = $totalShifts; }

    return $output;
}

for ($week=1; $week<15; $week++)
{
    $data = getShiftForWeek($week);
    echo "Week {$data['week']} - Week of {$data['monthDay']} = Shift {$data['shift']}<br />\n";
}

?>

 

Output:

Week 1 - Week of Jan 04 = Shift 5
Week 2 - Week of Jan 11 = Shift 6
Week 3 - Week of Jan 18 = Shift 1
Week 4 - Week of Jan 25 = Shift 2
Week 5 - Week of Feb 01 = Shift 3
Week 6 - Week of Feb 08 = Shift 4
Week 7 - Week of Feb 15 = Shift 5
Week 8 - Week of Feb 22 = Shift 6
Week 9 - Week of Mar 01 = Shift 1
Week 10 - Week of Mar 08 = Shift 2
Week 11 - Week of Mar 15 = Shift 3
Week 12 - Week of Mar 22 = Shift 4
Week 13 - Week of Mar 29 = Shift 5
Week 14 - Week of Apr 05 = Shift 6

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.