1

I have a TABLE events

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

with a number of records.

I generate an array with all unavailable dates

$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"];
}

The array $unavailable is like this: (2012-08-10, 2012-08-25) The array $days is like this: (3, 2)

I need an array like this: (2012-08-10, 2012-08-11, 2012-08-12, 2012-08-25, 2012-08-26)

Thank you!

1 Answer 1

1

Here is some code that generates what you want.

<?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);
?>

You could move this code into to while loop of your code to prevent creating the $unavailable and $days arrays

Sign up to request clarification or add additional context in comments.

1 Comment

"Vote Up requires 15 reputation" I have 8 :(

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.