I want to get the week numbers for given two dates i.e from 2012-01-01 to 2012-12-31.The week numbers should fall exactly in the range as specified above.Can u please give suggestions for doing this.
Something like this should work fine:
<?php
$startDateUnix = strtotime('2012-01-01');
$endDateUnix = strtotime('2013-01-01');
$currentDateUnix = $startDateUnix;
$weekNumbers = array();
while ($currentDateUnix < $endDateUnix) {
$weekNumbers[] = date('W', $currentDateUnix);
$currentDateUnix = strtotime('+1 week', $currentDateUnix);
}
print_r($weekNumbers);
?>
DEMO.
Output:
Array
(
[0] => 52
[1] => 01
[2] => 02
.........
[51] => 51
[52] => 52
)
Do something like this:
[REMOVED]
EDIT
<?php
for($w = strtotime($start_date); $w <= strtotime($end_date); $w += 7 * 24 * 3600)
{
echo date("W", $w) . '<br />';
}
?>
Something like this should do the job:
$start = '2012-01-01';
$end = '2012-12-31';
$dates = range(strtotime($start), strtotime($end),604800);
$weeks = array_map(function($v){return date('W', $v);}, $dates); // Requires PHP 5.3+
print_r($weeks);
I think you want something like this using DateTime:
$first_date = new DateTime();
$last_date = new DateTime('-50 weeks');
$days_array = array();
foreach(new DatePeriod($first_date, new DateInterval('P1D'), $last_date) as $date) {
$days_array[] = $date->format('W');
}
You can get the number of weeks between two dates using the weeks_between_dates() function below.
function weeks_between_dates($d1,$d2)
{
$t1 = strtotime($d1);
$t2 = strtotime($d2);
$out = array();
while ($now <= $t2) {
$out[] = date('W', $t1);
$t1 = strtotime('+1 week', $t1);
}
return $out;
}
print_r( weeks_between_dates('2015-01-01','2015-12-31') ); // 01..53
print_r( weeks_between_dates('2016-01-01','2016-12-31') ); // 01..52