2

I am creating a scheduler script that if I executed a task today my next task will be 6 months from today (Semi-Annual) but my problem is people has no activity on Sundays so it must be adjusted on Monday instead.

How can I add 6 month to today's date but exclude Sundays, I am using PHP and able to add 6 months using below code so far.

My code is this.

<?php echo  date('Y-m-d', strtotime('+6 months'));?>
Ravi Hirani
  • 6,511
  • 1
  • 27
  • 42
KaoriYui
  • 912
  • 1
  • 13
  • 43
  • this may helps http://stackoverflow.com/questions/2332681/add-number-of-days-to-a-date – LF00 May 05 '17 at 06:40
  • 2
    echo getdate(strtotime('+6 months'))['wday']==0 ? date('Y-m-d', strtotime('+6 months 1 day')) : date('Y-m-d', strtotime('+6 months')); Just do it in one line. I assume that you want to advance by one day if what you get when you add 6 months is a Sunday. – blokeish May 05 '17 at 06:42
  • @blokeish this was the simplest, It worked Thanks! – KaoriYui May 05 '17 at 06:57
  • @blokeish Nearly the same as my answer using the if statement, just as a oneliner – Jelmergu May 05 '17 at 07:09
  • @Jelmergu "coding is poetry" make it brief, interesting and meaningful. – blokeish May 05 '17 at 07:19
  • @blokeish qoute of the day, but for beginners my way might be slightly more understandable as it takes a bit of searching to find the oneliner for a the if else statement – Jelmergu May 05 '17 at 07:51

4 Answers4

0

As stated in the PHP.net date() documentation you can check for a specific day number.

So, using that knowledge you can either:

if (date("w", strtotime("+6 months")) == 0) {
        echo date("Y-m-d", strtotime("+6 months +1 days"));
    }
    else {
        echo date("Y-m-d", "+6 months");
    }

or something in the lines of Bilal Ahmed's answer using a switch

switch (date("w", strtotime("+6 months")) == 0) {
        case 0: // Sunday
            echo date("Y-m-d", strtotime("+6 months +1 days"));
        break;
        case 1: // Monday
        case 2: // Tuesday
        case 3: // Wednesday
        case 4: // Thursday
        case 5: // Friday
        case 6: // Saturday
            echo date("Y-m-d", "+6 months");
        break;
    }

which might be better if there is something to do for every different day as date() will not be executed every time you perform a case. (Note: I did not compare speeds)

EDIT: oneliner based on the comment of blokish on the askers post

echo date("w", strtotime("+6 months")) == 0 ? date("Y-m-d", "+6 months +1 days") : date("Y-m-d", "+6 months");
Jelmergu
  • 973
  • 1
  • 7
  • 19
0

Here's the answer: https://stackoverflow.com/a/12365635/6557808 Provides more than you're asking for and is pretty clear.

Community
  • 1
  • 1
Sylogista
  • 565
  • 3
  • 10
0

Here is a small function to it. Just pass from and end date

   <?php

        function number_of_working_days($from, $to) {
            $workingDays = [1, 2, 3, 4, 5, 6]; # date format = N (1 = Monday, ...)

            $from = new DateTime($from);
            $to = new DateTime($to);
            $to->modify('+1 day');
            $interval = new DateInterval('P1D');
            $periods = new DatePeriod($from, $interval, $to);

            $days = 0;
            foreach ($periods as $period) {
                if (!in_array($period->format('N'), $workingDays)) continue;
                $days++;
            }
            return $days;
        }

echo number_of_working_days('2017-05-06', '2017-05-08');


         ?>
Ashkar
  • 712
  • 6
  • 17
0

Complete Answer is here:--

function daysToAdd($startDate,$endDate)
{
$count=0;
While($startDate!=$endDate){
$lpcnt=0;
$begin = new DateTime($startDate);
$end = new DateTime($endDate);
$interval = DateInterval::createFromDateString('1 day');
$period = new DatePeriod($begin, $interval, $end);
foreach ( $period as $dt ){
  if($dt->format( "l" )=='Sunday'){
      $count++;
      $lpcnt++;
  }
}
$day = date('D', strtotime($endDate));
if($day=='Sun'){
    $count=$count+1;
    $lpcnt=$lpcnt+1;
}
$startDate=date('Y-m-d', strtotime($endDate . ' +1 day'));
$endDate=date('Y-m-d', strtotime($endDate . ' +'.$lpcnt.' day'));
if($startDate!=$endDate){
   daysToAdd($startDate,$endDate);
}else{
   if(date('D', strtotime($startDate))=='Sun') {
       $count=$count+1;
       break;
   }
}
}
return $count;
}
$currentDate=date('Y-m-d');
$postSixMonth=date('Y-m-d', strtotime("+5 months"));
$daysToadd=daysToAdd($currentDate,$postSixMonth);
$requiredDate=date('Y-m-d', strtotime($postSixMonth . ' +'.$daysToadd.' days'));
echo $requiredDate;exit;
  • Might be me, but isn't this a bit much to archieve just the adding of a single day, when the now plus 6 months is a Sunday? – Jelmergu May 05 '17 at 07:53