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.
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'; ...
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.
$dates = getBetweenDates('2021-11-01', '2021-11-10'); print_r($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.
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.
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"
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With