Reputation: 345
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;
?>
Upvotes: 1
Views: 612
Reputation: 345
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;
}
Upvotes: 0
Reputation: 9967
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;
?>
Upvotes: 0
Reputation: 219834
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");
Upvotes: 1