将assoc数组填入日期

时间:2015-03-01 11:29:46

标签: arrays

我需要使用key = date和值0填充一个关联数组$ pay_day,从开始日期$ d1到结束日期$ d2。

$d1 = strtotime("October 12 2014");
$d2 = strtotime("February 28 2015");
$startday = date('F j, Y', $d1 + (24 * 60 * 60));
echo "<h2>Startday is " . $startday . "</h2>";
$endday = date('F j, Y', $d2 + (24 * 60 * 60));
echo "<h2>Endday is " . $endday . "</h2>";
$n=0;
$d3=$d1;
while ($d3 <= $d2) {
    $d3 = date('F j, Y', ($d1 + ++$n*(24 * 60 * 60)));
    $pay_day[$d3] = 0;
}

1 个答案:

答案 0 :(得分:1)

$d1 = 'August 12 2014';
$d2 = 'February 28 2015';

$period = new \DatePeriod(
    \DateTime::createFromFormat('F d Y', $d1),
    new \DateInterval('P1D'), 
    \DateTime::createFromFormat('F d Y', $d2)->modify('+1 day'));

foreach($period as $date) {
  $d3 = $date->format('F j, Y');
  $pay_day[$d3] = 0;
}
print_r($pay_day);