Jump to content

Custom Week Numbers


jimmyborofan

Recommended Posts

I have a client who has an odd week numbering pattern, different Years have different week number starting dates (e.g. Academic starts in September, Tax year is April, etc etc)

 

I know you can get the week number using date("W") if I do this with todays date (2/JAN/10) I get 53 (it is the last day of the last week of the year, the new year starts tomorrow)

 

My clients trading year begins on Dec 25th, so I need to be able to work out week numbers from then.

 

Any pointers would be gratefully recieved.

 

Jimmy

Link to comment
Share on other sites

Not the best solution but works:

 

/**
* @param[in] $date UNIX timestamp
*/
function tradingWeek($date) {
  if (date('md',$date) <= 1224) {
$startDate = strtotime((date('Y',$date) - 1).'-12-25');
  } else {
$startDate = strtotime(date('Y',$date).'-12-25');
  }
  
  $secondsSince = $date - $startDate;
  $weeksSince = $secondsSince / (3600 * 24 * 7);
  $weekNumber = floor($weeksSince + 1);
  
  return($weekNumber);
}

echo tradingWeek(strtotime('2009-12-24')).PHP_EOL;
echo tradingWeek(strtotime('2009-12-25')).PHP_EOL;
echo tradingWeek(strtotime('2010-12-31')).PHP_EOL;
echo tradingWeek(strtotime('2010-01-01')).PHP_EOL;

Link to comment
Share on other sites

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.