Question

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

Was it helpful?

Solution

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top