0

I am trying to write a custom function for getting dates from week numbers but the requirement is that the week numbers are custom i.e. the week1 starts from 1st Week of March and the First Day of each week is Friday I trying to do this in PHP.

Your help or advice would be really helpful for writing this function. I have gone through some of functions here but doesnt actually fit my requirement.

Thanking You

vin
  • 3
  • 1
  • Can you give sample input and sample output of your function? – Patashu Apr 13 '13 at 12:15
  • This is a function Reference of this is Stackoverflow itself $week_number = 1; $year = 2013; for($day=1; $day<=7; $day++) { echo date('m/d/Y', strtotime($year."W".$week_number.$day))."\n"; } But here it takes the standard weeks and as said my week starts in march – vin Apr 13 '13 at 12:18
  • So week #1 can actually be less than 7 days? e.g. if 1st March falls on a Thursday, it will be a 1-day week? – Mark Baker Apr 13 '13 at 12:29
  • @MarkBaker thanks for your reply. Actually its bit complicated than that maybe for me :) For E.G. Month = March = 1st Month of Year 1st week starts from 1st March So, Week#1 = 1st March to 7th March. The Catch If 1st March = Thursday then 2nd March will be the start as the Start Date should always fall on Friday – vin Apr 13 '13 at 12:36
  • So week 1 starts on the first Friday on or after 1st March? – Mark Baker Apr 13 '13 at 12:39
  • @MarkBaker Yes the week #1 starts on Friday of March and then so on. Like each week start day is Friday. – vin Apr 13 '13 at 12:45

1 Answers1

0

This might give you a starting point:

$year = 2012;
$startDate = new \DateTime($year . '-03-01');
$startDate->modify('Friday');
echo $startDate->format('Y-m-d') , PHP_EOL;
$endDate = new \DateTime($year+1 . '-03-01');
$endDate->modify('Friday');
echo $endDate->format('Y-m-d') , PHP_EOL;

$interval = new \DateInterval('P1W');
$weekPeriod = new \DatePeriod ($startDate, $interval, $endDate);

foreach ($weekPeriod as $key => $weekDate) {
   echo 'Week #' , $key + 1 , ' starts on ';
   echo $weekDate->format('Y-m-d') , PHP_EOL;
}

You can use this to build an array, that you can then use as a lookup

Mark Baker
  • 209,507
  • 32
  • 346
  • 385
  • Thanks Mark. I will go through it and update you. Thanks for your quick help – vin Apr 13 '13 at 13:02
  • Yes you are right its very helpful to start off for what I want to achieve. Thanks a lot again. :) – vin Apr 13 '13 at 13:09