I need to add days (input type number + input type date) but the result must be an array so I can INSERT one after another into the Database.
Here's the code (After HTML Form submitted):
<?php $start_date = '2017-12-22'; $duration = '3'; $d = new DateTime($start_date); $t = $d->getTimestamp(); // loop for X days for($i=0; $i <= $duration; $i++){ // add 1 day to timestamp- $addDay = 86400; // get what day it is next day $nextDay = date('w', ($t + $addDay)); // if it's Saturday or Sunday get $i-1 if($nextDay === 6 || $nextDay === 7) { $i --; } // modify timestamp, add 1 day $t = $t + $addDay; $d->setTimestamp($t); $day_off = $d->format( 'Y-m-d' ). "<br />"; echo $day_off; $query = "INSERT SQL"; } ?>
From echo $day_off result I get:
2017-12-23 2017-12-24 2017-12-25 2017-12-26
Instead of 23, 24, 25, 26. I need to get the result below:
2017-12-22 2017-12-25 2017-12-26 2017-12-27
22 is the input date, start from 25 because 23 and 24 are Sat and Sun and weekends need to be excluded.
How can I achieve this result? I've been searching on the net but unfortunately, I couldn't find what I needed.
@C. Geek answer made it to works, but I have a more complex question here, since my account are not eligible to ask more question so I'll ask here.
So here's what I've tried so far (with @C. Geek answer) :
<?php
// loop for X days
for($i=0; $i < $duration; $i++){
$d = strtotime("$start_date +$i weekdays");
$t = strftime("%Y-%m-%d",$d);
$day_off[] = $t;
foreach($day_off as $dayoff) {
$data_holiday = mysqli_fetch_array(mysqli_query($con, "SELECT * FROM `holiday_master_data` WHERE `date` = '$dayoff' "));
}
$holiday[] = $data_holiday['date'];
$date = array_diff($day_off, $holiday);
$dayoff_ = $holiday;
?>
Start date : 2017-12-29 Duration : 5 days
From print_r($day_off); I'm getting this result :
Array ( [0] => 2017-12-29 ) Array ( [0] => 2017-12-29 [1] => 2018-01-01 ) Array ( [0] => 2017-12-29 [1] => 2018-01-01 [2] => 2018-01-02 )
And from print_r($holiday); I'm getting this result :
Array ( [0] => ) Array ( [0] => [1] => 2018-01-01 ) Array ( [0] => [1] => 2018-01-01 [2] => ) Array ( [0] => [1] => 2018-01-01 [2] => [3] => ) Array ( [0] => [1] => 2018-01-01 [2] => [3] => [4] => )
The national date fetched from database is 2018-01-01 with 5 looping result, the final date result I need to make are 29 Dec, 02 Jan 03 Jan and 04 Jan, 05 Jan.
Any help will be much appreciated. Thanks.