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
https://forums.phpfreaks.com/topic/186948-custom-week-numbers/
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
https://forums.phpfreaks.com/topic/186948-custom-week-numbers/#findComment-987223
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.