如何在开始日和结束日之间每天输出?

时间:2015-05-28 05:25:54

标签: php date

我想在开始日和结束日之间每天输出,例如:

$start_day = '20150530';
$end_day = '20150602';

//the output should be array('20150530', '20150531', '20150601', '20150602');
print_r(output_days($start_day, $end_day));

function output_days($start_day, $end_day) {
    // any idea?
}

谢谢。

5 个答案:

答案 0 :(得分:1)

<?php
$date_from = strtotime("10 September 2000");
$date_to = strtotime("15 September 2000");


$day_passed = ($date_to - $date_from); //seconds
$day_passed = ($day_passed/86400); //days

$counter = 1;
$day_to_display = $date_from;
while($counter < $day_passed){
    $day_to_display += 86400;
    echo $day_to_display;
    $counter++;
}
?>

答案 1 :(得分:1)

为什么人们写这么复杂?

 $start = mktime(0,0,0,9,10,2000);
 $end   = mktime(0,0,0,9,15,2000);
 while($start<=$end)
 {
   $output[]=date("Ymd",$start);
   $start+=86400;
 }

<强> Fiddle

答案 2 :(得分:0)

$start_day = '20150530'; $end_day = '20150602';

//the output should be array('20150530', '20150531', '20150601', '20150602'); print_r(output_days($start_day, $end_day));

function output_days($start_day, $end_day) { $strtotime_start=strtotime($start_day); $strtotime_end=strtotime($end_day); $totaldays=$strtotime_end-$strtotime_start; $x=0 $dates[0]=date('Ymd',$strtotime_star) while($x<$totaldays){ $dates[]=date('Ymd', $strtotime_start+86400); $x++; } $dates[]=date('Ymd',$strtotime_end); return $dates; }

希望这能解决您的问题

答案 3 :(得分:0)

这是你想要的吗?

function alldays($start_date,$end_date,$format_date="Y-m-d") {

    $seconds = strtotime($end_date) - strtotime($start_date);
    for ($i=0; $i < $seconds ; $i+=(60*60*24)) { 
        $dates[] = date($format_date, strtotime($start_date)+$i);
    }
    return $dates;
}
//Tiny test
print_r(alldays('1999-01-01','1999-02-01'));

答案 4 :(得分:0)

wrapper