1

I have a database called Holidays which has a date field to save holiday date called STARTTIME, and a duration field to save how many days is the holiday called DURATION. I want to collect all data from that table between some month and year, and print the date 1 by 1.

For Example, here's my data at database:

STARTTIME  | DURATION
2016-12-09 | 1
2016-12-15 | 5

How to print it in php 1 by 1 per date? So it will result:

2016-12-09
2016-12-15
2016-12-16
2016-12-17
2016-12-18
2016-12-19
2016-12-20

My code now is like this:

//startDate and endDate is get from my filter, this is only for example 
$startDate = '2016-01-01';
$endDate = '2016-01-31';
$sqlHol = "SELECT STARTTIME, DURATION FROM HOLIDAYS WHERE DATEVALUE(STARTTIME) BETWEEN DateAdd('m',-1,#$startDate#) AND #$endDate#";
$queryHol = $db->query($sqlHol);
$hols = $queryHol->fetchAll();
echo '<br />';
//testing to print out array result
print_r($hols);
//looping through data from database
foreach($hols as $hol)
{
    for($i=1; $i <= $hol[1]; $i++)
    {
        echo '<br />';
        $due = date_create($hol[0]);
        echo date_format($due, 'Y-m-d');
        $due = date_add(date_create($hol[0]), date_interval_create_from_date_string('1 days'));
        echo '<br />';
    }
}

And here's my result (the date doesn't increment):

2015-12-09

2015-12-15

2015-12-15

2015-12-15

2015-12-15

2015-12-15

2015-12-15

Anyone know how to create an output like I need?

3
  • You have two <br> tags in your for loop, try removing the first one.
    – lampwins
    May 18, 2016 at 2:59
  • Also don't make a habit of executing queries this way, it's insecure. Search PDO.
    – lampwins
    May 18, 2016 at 3:01
  • @lampwins sorry, it's just for enter... the problem is my date isn't counting... as You can see it always 15... I can't use PDO, I don't know why if I use ms.access I can't use PDO... and this apps only use for a person, so I don't really thought about secure... May 18, 2016 at 3:03

1 Answer 1

1

It looks like you are writing the due date before you increment it. Try using the loop counter as a way to keep track of how many times you have incremented the date.

foreach($hols as $hol)
{
    for($i=0; $i <= $hol[1]-1; $i++)
    {
        echo '<br />';
        $due = date_create($hol[0]);
        $due = date_add(date_create($hol[0]), date_interval_create_from_date_string(i.' days'));
        echo date_format($due, 'Y-m-d');
        echo '<br />';
    }
}
4
  • it's better... but the result of mine, at day 9, it counting too... so the result is like these: 2015-12-09, 2015-12-10, 2015-12-11, 2015-12-15, 2015-12-16, 2015-12-17, 2015-12-18, 2015-12-19, 2015-12-20. May 18, 2016 at 3:59
  • No problem, I fixed this... Your code at: $hol[1]+1 supposed to be $hol[1]. I believe there's no other error... thank you so much May 18, 2016 at 4:02
  • My mistake, change $hol[1]+1 to $hol[1]-1
    – Daniel
    May 18, 2016 at 4:04
  • Exactly... I've just wrote that.. :D May 18, 2016 at 4:04

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.