5

I have date ranges called from and to.I want to convert it to weeks. Suppose from date is 1-10-2014 and to date is 31-10-2014

Then result is:

1st week : 01-10-2014 to 04-10-2014 2nd : 05-102014 to 11-10-2014 3rd : 12-10-2014 to 18-10-2014 4th : 19-10-2014 to 25-10-2014 5th : 26-10-2014 to 31-10-2014

In php.I try several code but that didn't given the absolute result only give 01 to 7 , 8 to 14 etc. Pls help.

I already try answers from

Get the date of one week from today with PHP in stack overflow


date("Y-m-d",strtotime("+1 week"));
Adarsh M Pallickal
  • 813
  • 3
  • 16
  • 37

3 Answers3

12

This snippet uses Sunday as the first day of the week:

    $start = new DateTime('2014-10-01');
    $end = new DateTime('2014-10-31 23:59');
    $interval = new DateInterval('P1D');
    $dateRange = new DatePeriod($start, $interval, $end);

    $weekNumber = 1;
    $weeks = array();
    foreach ($dateRange as $date) {
        $weeks[$weekNumber][] = $date->format('Y-m-d');
        if ($date->format('w') == 6) {
            $weekNumber++;
        }
    }

Each week will have all the days in it.

If you just want the first and last days of each week then you can just use array_shift and array_pop to get them. For example, for the first week you can use:

    $wk1Start = array_shift($weeks[1]); //gives you first day of week 1
    $wk1End = array_pop($weeks[1]); // give you the last day of week 1

If you want the start and end dates for each week, here is a way of doing it:

    $ranges = array_map(function($week) {
        return 'start: ' . array_shift($week) 
            . ', end: ' . array_pop($week); },
    $weeks);

This is the output of $ranges for me:

    Array
    (
        [1] => start: 2014-10-01, end: 2014-10-04
        [2] => start: 2014-10-05, end: 2014-10-11
        [3] => start: 2014-10-12, end: 2014-10-18
        [4] => start: 2014-10-19, end: 2014-10-25
        [5] => start: 2014-10-26, end: 2014-10-31
    )
ymas
  • 474
  • 6
  • 10
  • what is the case that fails? – ymas Nov 25 '14 at 05:38
  • when i print $weeks got Array ( [1] => Array ( [0] => 2014-10-01 [1] => 2014-10-02 [2] => 2014-10-03 [3] => 2014-10-04 ) [2] => Array ( [0] => 2014-10-05 [1] => 2014-10-06 [2] => 2014-10-07 [3] => 2014-10-08 [4] => 2014-10-09 [5] => 2014-10-10 [6] => 2014-10-11 ) [3] => Array ( [0] => 2014-10-12 [1] => 2014-10-13 [2] => 2014-10-14 [3] => 2014-10-15 [4] => 2014-10-16 [5] => 2014-10-17 [6] => 2014-10-18 ) etc – Adarsh M Pallickal Nov 25 '14 at 05:42
  • That is as expected, it will show you all the days in the week. If you just want the start and end dates, shift and pop each array. – ymas Nov 25 '14 at 05:42
  • If you look at each array in `$weeks`, the first element and the last element are the date ranges you specified. $start = `array_shift($weeks[1])` will give you the first day of the first week and `array_pop($weeks[1])` will give you the last day of the first week. Similarly for the other weeks. – ymas Nov 25 '14 at 05:48
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/65556/discussion-between-adarsh-m-pallickal-and-ymas). – Adarsh M Pallickal Nov 25 '14 at 05:52
  • @ymas can you pls, tell me how can I get week number of the year instead of 1, 2, 3 – TechNerdXp Dec 14 '18 at 23:29
  • @NadeemGorsi you can start the counter from $date->format("W"), where date is the first day in your range, is this what you mean? – ymas Dec 15 '18 at 06:48
  • yes @ymas I did that even we don't need a counter using just like this `$weeks["week_".$date->format('W')][] = $date->format('Y-m-d');` . Thanks for replay. Happy Coding! – TechNerdXp Dec 15 '18 at 17:41
  • Hello @NadeemGorsi bear in mind the first day of the week if you're going to do it like that. Good job! – ymas Dec 15 '18 at 18:00
  • yes I noticed the first day of the year is first day of week. Thankx @ymas – TechNerdXp Dec 15 '18 at 19:46
1

You can easily change the week starting day (set to Monday at the moment):

$fromDate = '2018-05-03';
$toDate = '2018-08-11';
$result = getWeekRanges($fromDate, $toDate);
array_walk_recursive($result, 'applyFormat');
echo '<pre>';
print_r($result);
echo '</pre>';

function getWeekRanges($start, $end) {
    $timeStart = strtotime($start);
    $timeEnd   = strtotime($end);
    $out       = [];
    $milestones[] = $timeStart;
    $timeEndWeek = strtotime('next Monday', $timeStart);
    while ($timeEndWeek < $timeEnd) {
        $milestones[] = $timeEndWeek;
        $timeEndWeek = strtotime('+1 week', $timeEndWeek);
    }
    $milestones[] = $timeEnd;
    $count = count($milestones);
    for ($i = 1; $i < $count; $i++) {
        if( $i == $count - 1) {
            $out[] = [
                'start' => $milestones[$i - 1],
                'end'   => $milestones[$i]
            ];
        }else{
            $out[] = [
                'start' => $milestones[$i - 1],
                'end'   => $milestones[$i] - 1
            ];
        }
    }
    return $out;
}


function applyFormat(&$item) {
    $item = date('Y-m-d', $item);
}
scopchanov
  • 7,966
  • 10
  • 40
  • 68
Wes
  • 56
  • 3
0

Try

$start_date = date('Y-m-d', strtotime('2014-10-01'));
$end_date = date('Y-m-d', strtotime('2014-10-31'));
$i=1;
for($date = $start_date; $date <= $end_date; $date = date('Y-m-d', strtotime($date. ' + 7 days'))) {
    echo getWeekDates($date, $start_date, $end_date, $i);
    echo "\n";
    $i++;
}

function getWeekDates($date, $start_date, $end_date, $i) {
    $week =  date('W', strtotime($date));
    $year =  date('Y', strtotime($date));
    $from = date("Y-m-d", strtotime("{$year}-W{$week}+1")); //Returns the date of monday in week
    if($from < $start_date) $from = $start_date;
    $to = date("Y-m-d", strtotime("{$year}-W{$week}-7"));   //Returns the date of sunday in week
    if($to > $end_date) $to = $end_date;
    echo "$i th ".$from." to ".$to.'<br>';
}  

output :-

1 th 2014-10-01 to 2014-10-05
2 th 2014-10-06 to 2014-10-12
3 th 2014-10-13 to 2014-10-19
4 th 2014-10-20 to 2014-10-26
5 th 2014-10-27 to 2014-10-31
Rakesh Sharma
  • 13,680
  • 5
  • 37
  • 44