1

I want it to automatically add days until Monday if someone choose Friday. Imagine $leavefrom is 3-1-2014 which is Thursday, and $leaveto is 3-2-2014 is Friday. $totaldays are calculated based on the date. Therefore it is 2 days.

<?php
$x = 0;

$date1 = str_replace('-', '/', $leavefrom);
$date2 = str_replace('-', '/', $leaveto);

while ($x < $totaldays) {

    $tomorrow = date('l', strtotime($date1 ."+1 days"));

    //$tomorrow = date("m-d-Y", strtotime( $date1 ."+1 days" ));
    $getday = date('D', strtotime($tomorrow));
    $x++;
    if ($getday == "Sunday" || $getday = "Saturday") {
        $tomorrow = date("m/d/Y", strtotime( $tomorrow ."+1 days" ));
    }
    $tomorrow = date("m/d/Y", strtotime( $tomorrow ."+1 days" ));
}

echo $tomorrow;
?>
John Conde
  • 217,595
  • 99
  • 455
  • 496
Furry
  • 340
  • 1
  • 4
  • 22
  • 1
    what exactly is the problem? and could you try to be a bit more clear with what you want? (I see saturday, monday, add up till friday... I'm a bit confused ;)) – giorgio Mar 18 '14 at 15:26
  • Ok, you see, I want to create a simple leave system, so if someone apply the leave on those 2 dates mentioned above, both date are Thursday and Friday, so the user who apply this leave will start work on Monday. I need it's date for Monday skipping Saturday and Sunday. So they will start working on 5-3-2013. Get what I mean? Hehe sorry, poor english. – Furry Mar 18 '14 at 15:29

3 Answers3

1

If you're just trying to skip weekends just check to see if $date2 is on a weekend, if so, skip ahead to the next Monday.

$date2 = DateTime::CreateFromFormat('n-j-Y', $leaveto);
if (in_array($date2->format('l'), array('Sunday', 'Saturday'))) {
    $date2->modify('next Monday');
}
echo $date2->format("m/d/Y");
John Conde
  • 217,595
  • 99
  • 455
  • 496
0

Try changing the if ($getday == "Sunday" || $getday = "Saturday") into a while, instead, and get rid of the last $tomorrow = .... Something like this:

<?php
$x = 0;

$date1 = str_replace('-', '/', $leavefrom);
$date2 = str_replace('-', '/', $leaveto);

while ($x < $totaldays) {

    $tomorrow = date('l', strtotime($date1 ."+1 days"));
    $x++;

    $getday = date('D', strtotime($tomorrow));
    while ($getday == "Sunday" || $getday = "Saturday") {
        $tomorrow = date("m/d/Y", strtotime( $tomorrow ."+1 days" ));
        $getday = date('D', strtotime($tomorrow));
    }

}

echo $tomorrow;
?>
brianmearns
  • 9,581
  • 10
  • 52
  • 79
  • You sure this code is correct? I get infinite loop. – Furry Mar 18 '14 at 15:52
  • Nope, not at all. You didn't give us enough to really test with so I don't know if it works or not. Why don't you drop an `echo` inside the loop to print out the value of `$tomorrow` and `$getday`, see why it's not exiting. – brianmearns Mar 18 '14 at 16:04
0

I found solution after 3 hours of head bang on the wall for being stupid, below is my code:

while ($daysloop <= $totaldays) {
$tomorrow1 = date("m/d/Y", strtotime( $tomorrow1 ."+1 days" ));
$dayofweek = date('w', strtotime($tomorrow1));

if ($dayofweek == 0 || $dayofweek == 6) {
$weekends = $weekends + 1;
}
$daysloop++;
}

if ($totaldays == 0) {
$totaldays = $totaldays - $weekends + 1;
}
else {
$totaldays = $totaldays - $weekends;
}
Furry
  • 340
  • 1
  • 4
  • 22