1

我有一张桌子events

`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`data` date DEFAULT NULL,
`days` int(2) DEFAULT NULL,
PRIMARY KEY (`id`)

与许多记录。

我生成一个包含所有不可用日期的数组

$sql = "SELECT * FROM events WHERE `data` LIKE '".$cYear."-".$cMonth."-%'";
$sql_result = mysql_query ($sql, $connection ) or die ('request "Could not execute SQL query" '.$sql);
while ($row = mysql_fetch_assoc($sql_result)) {
$unavailable[] = $row["data"];
$days[] = $row["days"];
}

数组 $unavailable 是这样的: (2012-08-10, 2012-08-25) 数组 $days 是这样的: (3, 2)

我需要这样的数组: (2012-08-10, 2012-08-11, 2012-08-12, 2012-08-25, 2012-08-26)

谢谢!

4

1 回答 1

1

这是一些生成您想要的代码的代码。

<?php

$unavailable = array('2012-08-10', '2012-08-25');
$days = array(3, 2);

$dates = array();
for ($i = 0; $i < count($days); $i++)
{
    $tm = strtotime($unavailable[$i]);
    for ($d = 0; $d < $days[$i]; $d++)
    {
        $dates[] = date('Y-m-d', $tm + $d * 24 * 60 * 60);
    }
}
print_r($dates);
?>

您可以将此代码移动到代码的 while 循环中,以防止创建 $unavailable 和 $days 数组

于 2012-08-16T10:05:27.663 回答