1

I'd like display dates by week number between giving 2 dates like example below. Is this possible in PHP?

if the dates are 2010-12-01 thru 2010-12-19, it will display it as follows.

week-1  
 2010-12-01  
 2010-12-02  
 2010-12-03  
 2010-12-04  
 2010-12-05  
 2010-12-06  
 2010-12-07  
week-2    
 2010-12-08  
 2010-12-09  
 2010-12-10  
 2010-12-11  
 2010-12-12  
 2010-12-13  
 2010-12-14  
week-3  
 2010-12-15  
 2010-12-16  
 2010-12-17  
 2010-12-18  
 2010-12-19  
and so on...  

I use mysql. It has startdate end enddate fields. thank you in advance.

I can get how many weeks in those giving 2 dates and display them using a

datediff('ww', '2010-12-01', '2010-12-19', false); I found on the internet.    

And I can display dates between two dates as follows. But I am having trouble grouping them by week.

$sdate = "2010-12-01";
$edate = "2010-12-19";

$days = getDaysInBetween($sdate, $edate);
foreach ($days as $val)  
{  
echo $val;  
} 

function getDaysInBetween($start, $end) {   
// Vars   
$day = 86400; // Day in seconds   
$format = 'Y-m-d'; // Output format (see PHP date funciton)   
$sTime = strtotime($start); // Start as time   
$eTime = strtotime($end); // End as time   
$numDays = round(($eTime - $sTime) / $day) + 1;   
$days = array();   

// Get days   
for ($d = 0; $d < $numDays; $d++) {   
$days[] = date($format, ($sTime + ($d * $day)));   
}   

// Return days   
return $days;   
}
RobertPitt
  • 56,863
  • 21
  • 114
  • 161
librium
  • 35
  • 5

5 Answers5

1

You will definitely need this: Simplest way to increment a date in PHP?. Write a forloop and increment the day every time. You will also need the DateTime class and functions as date. Indeed asking for date('W', yourDateHere) is a nice idea.

You will get something like this (pseudocode)

$startDate;
$endDate;

$nrOfDays = dateDiffInDays($endDate, $startDate);
$currentWeek = date('W',$startDate);
for($i = 0; $i < $nrOfDays; $i++)
{
    $newDay = date('+$i day', $startDate); // get the incremented day
    $newWeek = date('W', $newDay); // get the week of the new day

    if($newWeek != $currentWeek) // check if we must print the new week, or if we are still in the current
    print $newWeek;

    print $newDay; // print the day
}

Hope this helps. Good luck.

Community
  • 1
  • 1
Marnix
  • 6,384
  • 4
  • 43
  • 78
1

Tools sufficient to do the job:

  • strtotime('2010-11-23') - to get a timestamp from a date
  • strtotime('+1 day', $someTimestamp) - to get the next day
  • date('W', $someTimestamp) - to get the week number (if you want to group by ISO week number)
  • array_chunk($orderedListOfSuccessiveDates, 7) - to split in groups of seven days (if you don't want to group by ISO week number)

Warning: Never, ever increment days by adding 86400 to the timestamp! That is the easiest way to break everything when Daylight Saving comes along. Either use the strtotime function or the DateTime class.

Alin Purcaru
  • 43,655
  • 12
  • 77
  • 90
1

New answer.

$current_date = strtotime('2010-12-01');
$end_date = strtotime('2010-12-19');
$day_count = 0;
$current_week = null;
do {
    if ((int)($day_count / 7) + 1 != $current_week) {
        $current_week = (int)($day_count / 7) + 1;
        echo 'week-'.$current_week.'<br />';
    }
    echo date('Y-m-d', $current_date).'<br />';
    $current_date = strtotime('+1 day', $current_date);
    $day_count ++;
} while ($current_date <= $end_date);
dqhendricks
  • 19,030
  • 11
  • 50
  • 83
1

Here you go. Although this is with weeks starting on sundays (just change it to monday if need be). And it doesnt work if the dates arent in the same year. But it should be pretty easy to fix that. If not_same_year then ...

$start_date = mktime(0, 0, 0, 12, 01, 2010);
$start_date_week_number = (int) date("W", $start_date);
$end_date = mktime(0, 0, 0, 12, 19, 2010);
$end_date_week_number = (int) date("W", $end_date);
$n = $start_date_week_number;
$w = 1;
$date = $start_date;

while($n <= $end_date_week_number) {
    echo("<strong>Week " . $w . "</strong><br />");
    $s = 0;
    $e = 6;
    if($n == $start_date_week_number) $s = (int) date("w", $start_date);
    elseif($n == $end_date_week_number) $e = (int) date("w", $end_date);
    while($s <= $e) {
        echo(date("j-m-y", $date) . "<br />");
        $c_date = getdate($date);
        $date = mktime($c_date['hours'], $c_date['minutes'], $c_date['seconds'], $c_date['mon'], $c_date['mday'] + 1, $c_date['year']);
        $s++;
    }
    $n++; $w++;
}

DEMO HERE

Edit: just fixed it when I realized you wanted to count the weeks (not get the actual week number)...

o1iver
  • 1,805
  • 2
  • 17
  • 23
1
    $startDate = new DateTime('2010-01-01');
    $endDate = new DateTime('2010-01-14');
    $weeksDays = getWeeksDaysBetween($startDate, $endDate);

    foreach($weeksDays as $week => $days)
    {
      echo "Week $week<ul>";
      foreach($days as $day){
        echo "<li>$day</li>";
      }
      echo "</ul>";
    }

  function getWeeksDaysBetween($startDate, $endDate)
  {
    $weeksDays = array();   
    $dateDiff = $endDate->diff($startDate);
    $fullDays = $dateDiff->d;
    $numWeeks = floor($fullDays / 7) + 1;
    $weeksDays[1][] = $startDate->format('Y-m-d');
    for ($i = 1; $i <= $fullDays; $i++)
    {
      $weekNum = floor($i / 7) + 1;
      $dateInterval  = DateInterval::createFromDateString("1 day");
      $weeksDays[$weekNum][] = $startDate->add($dateInterval)->format('Y-m-d');
    }
    return $weeksDays;
  }
James
  • 4,117
  • 2
  • 18
  • 13
  • I wish I could use new PHP's DateTime class. But My PHP does not have it yet. Hopefully in the future :) – librium Jan 17 '11 at 16:41