我有这个函数返回一个年,月和日的数组但是我需要用几周替换几天,所以每个月的数组将包含几周。它并不那么容易。
$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;
}
目前的结果是:
[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
(
我需要这样的东西:
[2013] => Array
(
[October] => Array
(
[0] => week 1
[1] => week 2
[2] => week 3
[3] => week 4
[3] => week 5
答案 0 :(得分:1)
这个函数应该这样做,我使用strtotime代替DateTime类,但是
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"));
答案 1 :(得分:1)
不确定这是否是您想要的,但是,嘿嘿。
$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;
}
将返回
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
)
)
)