Reputation: 1931
I have this function that returns an array of years, months and days but I need to replace the days with weeks, so each month array will contain the weeks. Its not that easy.
$start_date = '2007-03-24';
$end_date = '2009-06-26';
year_month($start_date, $end_date);
//Returns an array containing the years, months and days between two dates
public function year_month($start_date, $end_date)
{
$begin = new DateTime( $start_date );
$end = new DateTime( $end_date);
$end->add(new DateInterval('P1D')); //Add 1 day to include the end date as a day
$interval = new DateInterval('P1D');
$period = new DatePeriod($begin, $interval, $end);
$aResult = array();
foreach ( $period as $dt )
{
$aResult[$dt->format('Y')][$dt->format('F')][$dt->format('j')] = $dt->format('D');
}
return $aResult;
}
Currently results in:
[2013] => Array
(
[October] => Array
(
[15] => Tue
[16] => Wed
[17] => Thu
[18] => Fri
[19] => Sat
[20] => Sun
[21] => Mon
[22] => Tue
[23] => Wed
[24] => Thu
[25] => Fri
[26] => Sat
[27] => Sun
[28] => Mon
[29] => Tue
[30] => Wed
[31] => Thu
)
[November] => Array
(
[1] => Fri
[2] => Sat
[3] => Sun
[4] => Mon
[5] => Tue
[6] => Wed
[7] => Thu
[8] => Fri
[9] => Sat
[10] => Sun
[11] => Mon
[12] => Tue
[13] => Wed
[14] => Thu
[15] => Fri
[16] => Sat
[17] => Sun
[18] => Mon
[19] => Tue
[20] => Wed
[21] => Thu
[22] => Fri
[23] => Sat
[24] => Sun
[25] => Mon
[26] => Tue
[27] => Wed
[28] => Thu
[29] => Fri
[30] => Sat
)
[December] => Array
(
I need something like this :
[2013] => Array
(
[October] => Array
(
[0] => week 1
[1] => week 2
[2] => week 3
[3] => week 4
[3] => week 5
Upvotes: 1
Views: 2473
Reputation: 32117
Not sure if this is what you're wanting but, hey ho.
$start_date = '2007-03-24';
$end_date = '2009-06-26';
print_r(year_month($start_date, $end_date));
//Returns an array containing the years, months and week numbers between two dates
function year_month($start_date, $end_date)
{
$begin = new DateTime( $start_date );
$end = new DateTime( $end_date);
$end->add(new DateInterval('P1D')); //Add 1 day to include the end date as a day
$interval = new DateInterval('P1W'); //Add 1 week
$period = new DatePeriod($begin, $interval, $end);
$aResult = array();
foreach ( $period as $dt )
{
$aResult[$dt->format('Y')][$dt->format('F')][] = "Week ".$dt->format('W');
}
return $aResult;
}
Will return
Array
(
[2007] => Array
(
[March] => Array
(
[0] => Week 12
[1] => Week 13
)
[April] => Array
(
[0] => Week 14
[1] => Week 15
[2] => Week 16
[3] => Week 17
)
[May] => Array
(
[0] => Week 18
[1] => Week 19
[2] => Week 20
[3] => Week 21
)
[June] => Array
(
[0] => Week 22
[1] => Week 23
[2] => Week 24
[3] => Week 25
[4] => Week 26
)
[July] => Array
(
[0] => Week 27
[1] => Week 28
[2] => Week 29
[3] => Week 30
)
)
)
Upvotes: 1
Reputation: 2400
This function should do it, i use strtotime instead of the DateTime classes though
function year_month($start_date,$end_date){
$current_date = strtotime($start_date);
$end_date = strtotime($end_date);
$out=array();
while($current_date<=$end_date){
$out[date("Y",$current_date)][date("F",$current_date)][date("W",$current_date)][]=date("D",$current_date);
$current_date=strtotime("+1 days",$current_date);
}
return $out;
}
print_r(year_month("2007-03-24","2009-06-26"));
Upvotes: 1