Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display all the week numbers between two dates in PHP [duplicate]

Tags:

php

Can anybody tell how to display all the week numbers that are covered between two dates in PHP.The dates may be of different year.

IF i am using start date as "2011-09-16" and end date as "2011-09-21" it will show me week 37 and 38.

like image 800
Sitansu Avatar asked Sep 21 '11 07:09

Sitansu


People also ask

How can I calculate the number of weeks between two dates in PHP?

php function week_between_two_dates($date1, $date2) { $first = DateTime::createFromFormat('m/d/Y', $date1); $second = DateTime::createFromFormat('m/d/Y', $date2); if($date1 > $date2) return week_between_two_dates($date2, $date1); return floor($first->diff($second)->days/7); } $dt1 = '1/1/2014'; $dt2 = '12/31/2014'; ...

How do I find the week number between two dates?

To calculate the number of weeks between two dates, start by counting the number of days between the start and end date. Then, divide that number by 7 days per week.

How can I print the date between two dates in PHP?

$dates = getBetweenDates('2021-11-01', '2021-11-10'); print_r($dates);

How do I find the date between two dates?

We can get the dates between two dates with single method call using the dedicated datesUntil method of a LocalDate class. The datesUntill returns the sequentially ordered Stream of dates starting from the date object whose method is called to the date given as method argument.


1 Answers

You could use something like this...

$startTime = strtotime('2011-12-12');
$endTime = strtotime('2012-02-01');

$weeks = array();

while ($startTime < $endTime) {  
    $weeks[] = date('W', $startTime); 
    $startTime += strtotime('+1 week', 0);
}

var_dump($weeks);

CodePad.

Output

array(8) {
  [0]=>
  string(2) "50"
  [1]=>
  string(2) "51"
  [2]=>
  string(2) "52"
  [3]=>
  string(2) "01"
  [4]=>
  string(2) "02"
  [5]=>
  string(2) "03"
  [6]=>
  string(2) "04"
  [7]=>
  string(2) "05"
}
like image 134
alex Avatar answered Oct 07 '22 04:10

alex