-3

I would like to get a list of dates in the following format for a given year;

array:4 [▼
  "04" => "25 Jan 2016 - 31 Jan 2016"
  "03" => "18 Jan 2016 - 24 Jan 2016"
  "02" => "11 Jan 2016 - 17 Jan 2016"
  "01" => "4 Jan 2016 - 10 Jan 2016"
]

The array key shows the week number. The array value shows the dates.

If the year given is equal to the current year then I only want it to show dates up until the current week.

How can I achieve this?

V4n1ll4
  • 5,973
  • 14
  • 53
  • 92
  • 3
    What you have tried so far? – Muhammad Hassaan Jan 26 '16 at 08:54
  • Please read [What topics can I ask about](http://stackoverflow.com/help/on-topic) and [How to ask a good question](http://stackoverflow.com/help/how-to-ask) And [the perfect question](http://codeblog.jonskeet.uk/2010/08/29/writing-the-perfect-question/) You have to show that you have made some effort to solve your own problem. – RiggsFolly Jan 26 '16 at 08:55
  • 1
    No one will write you the whole algorithm, you have to do some stuff on your own – FKutsche Jan 26 '16 at 08:55
  • Possible duplicate of [How print all the weeks in an year (or first monday of year)](http://stackoverflow.com/questions/2099522/how-print-all-the-weeks-in-an-year-or-first-monday-of-year) – Huelfe Jan 26 '16 at 08:57

1 Answers1

0

This user defined getDates() function which takes weeknumber and year as input will return the desired result.

<?php

echo "<pre>";
print_r(getDates('4', '2016'));
echo "</pre>";


function getDates($weekNumber, $year) {
    $returnArr = array();

    for($i=$weekNumber; $i >= 1; $i--) {
        $wno = $i<=9 ? "0".$i : $i;

        $calStr = $year . "W" . $wno;

        $startDate = date('d M Y',strtotime($calStr));

        $endDate = date('d M Y', strtotime($startDate. ' + 6 day'));

        $weekDays = $startDate . ' - ' . $endDate;

        $returnArr[$wno] = $weekDays;
    }

    return $returnArr;
}

Output

Array
(
    [04] => 25 Jan 2016 - 31 Jan 2016
    [03] => 18 Jan 2016 - 24 Jan 2016
    [02] => 11 Jan 2016 - 17 Jan 2016
    [01] => 04 Jan 2016 - 10 Jan 2016
)
Raman Sahasi
  • 30,180
  • 9
  • 58
  • 71