All date combinations between a fixed period (PHP / Loops)
This sounds like a silly question but I am struggling to think of how best to tackle it.
I have 2 dates, lets say 15-03-2012 to 19-03-2012. I need to extract all combinations working down from all 4 days开发者_开发知识库 together to 4 individual days, output like this (formatting change for simplicity):
15, 16, 17, 18
15,16, 17
16, 17, 18
15, 16
16, 17
17, 18
15
16
17
18
I am looking for the best looping method, the date range will vary in length, the format isn't relevant but it needs to be php dates from which I will extract the exact format required. I will be using each date set to execute code at each iteration so has to be dates/loops.
Create two Date objects of the values you have, write a while loop that runs until the start variable (your start date) is equal to the end variable (your end date) and you should be done.
The general idea:
$day = 24 * 60 * 60;
for($startDate = $dateA; $startDate <= $dateB; $startDate += day)
{
for($endDate = $startDate; $endDate <= $dateB; $endDate += day)
{
for($dateIndex = startDate; $dateIndex <= $endDate; $dateIndex += day)
{
$output = getDate($dateIndex);
echo($output['month'] . $output['day'] . ',');
}
echo '</br>';
}
}
精彩评论