开发者

How to get the date between 2009-12-25 and 2009-12-30 in PHP?

开发者 https://www.devze.com 2022-12-15 00:10 出处:网络
How to implement the function 开发者_JAVA百科? So it\'ll output an array containing 2009-12-25 2009-12-26 2009-12-27 2009-12-28 2009-12-29 2009-12-30?$startdate = strtotime(\"2009-12-25\");

How to implement the function 开发者_JAVA百科?

So it'll output an array containing 2009-12-25 2009-12-26 2009-12-27 2009-12-28 2009-12-29 2009-12-30?


$startdate = strtotime("2009-12-25");
$enddate = strtotime("2009-12-30");
$steps = "1 day"; 

# Start
$date = $startdate;
while ($date <= $enddate)  
  {   echo date("Y-m-d", $date)."<br>";
      $date = strtotime ("+".$steps, $date); // can be slow with large arrays, 
                                             // you can also just add 60*60*24 seconds
                                             // to $date

   }


Besides you can use approach similar to the first answer.

 $startDate = mktime(0, 0, 0, 12, 25, 2009);
 $endDate = mktime(0, 0, 0, 12, 30, 2009);
 for ($i=$startDate; $i<=$endDate; $i=$i+86400) {
   $timeArray[]=date("Y-m-d", $i);
 }

Result:

array (
  0 => '2009-12-25',
  1 => '2009-12-26',
  2 => '2009-12-27',
  3 => '2009-12-28',
  4 => '2009-12-29',
  5 => '2009-12-30',
)


As of php 5.3 you can also use the DatePeriod class.


Use mktime with an incrementing variable for the day:

$array = array();
for ($x = 25; $x <= 30; $x++) {
    $array[] = date('Y-m-d', mktime(0, 0, 0, 12, $x, 2009));
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号