vin
vin

Reputation: 3

Getting Dates from Week Numbers - Year Starts from March - Week 1 = March 1st week - PHP

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

Upvotes: 0

Views: 311

Answers (1)

Mark Baker
Mark Baker

Reputation: 212522

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

Upvotes: 0

Related Questions