-1

How can I get 48 hours excluding weekends? For example today is 2020-01-31 23:00:00, I add 48 hours it should be 2020-02-04 23:00:00 but when I tried this code:

$time = strtotime('+48 hour');

while (date('w', $time) % 6 == 0) {
    $time = strtotime('+1 day', $time);
}

$date = date('Y-m-d H:i:s', $time);

It gave me 2020-02-03 23:00:00.

nalply
  • 26,770
  • 15
  • 78
  • 101
Darmawan
  • 1
  • 3
  • Is the weekend defined as Saturday from 00:00 to Sunday at 23:59? – Qirel Feb 03 '20 at 07:08
  • @Qirel yes, sir. – Darmawan Feb 03 '20 at 07:10
  • Is not $time = strtotime($time.' + 1 days'); ? – Simone Rossaini Feb 03 '20 at 07:14
  • @SimoneRossaini i tried but it gives me 1970-01-01 07:00:00 – Darmawan Feb 03 '20 at 07:24
  • Change the while to just if And add 2 days directly. – slepic Feb 03 '20 at 07:32
  • @slepic 48 is parameter, I can change anytime I want. For another example, today is 2020-01-31 23:00:00 when I give 2 hours it should be 2020-01-03 01:00:00 – Darmawan Feb 03 '20 at 07:40
  • That's irelevant. Weeknd Always has 48 Hours. Only exception would be if input Is on Weeknd already than you have to skip just the non past portion of the weekend. Or if you wanna add more than 7 days than you can hit multiple weekends And have to treat it differently too. – slepic Feb 03 '20 at 08:04
  • Can you explain what you exactly need ? - I understood like, after adding 48hrs (which is 2 days to the current date) if it comes as Saturday, then add 1day more. Is that correct ? – smilyface Feb 03 '20 at 08:56
  • @smilyface yes, so what I want is when I'm adding any numbers of hours it's always ignoring the weekends. For example, today is 2020-01-31 23:00:00 (Friday) when I give 2 hours it should be 2020-01-03 01:00:00 (Monday). Directly it's ignoring the 2020-01-01 01:00:00 (Saturday) and 2020-01-02 01:00:00 (Sunday) because that is weekend. – Darmawan Feb 03 '20 at 09:07
  • For your information, in this case I am developing IT Helpdesk Application, there are so many categories that each category has an SLA, and my SLA uses hours and SLA is only valid for weekdays. – Darmawan Feb 03 '20 at 09:22
  • Cool, Better include this point in your post (edit) before you mention about the code. Better to understand what actually you are trying to do. – smilyface Feb 03 '20 at 10:06
  • Does this answer your question? [how can i exclude saturday and sunday when i generate dates](https://stackoverflow.com/questions/21085285/how-can-i-exclude-saturday-and-sunday-when-i-generate-dates) – smilyface Feb 03 '20 at 10:07
  • Reading your question again it's confusing `today is 2020-01-31 23:00:00, I add 48 hours it should be 2020-02-04 23:00:00`. Do you really mean this ? 48 hrs means just 2 days + 2 nights. See the dates it's 4days after 31st. Also 4th Feb is Tuesday. – smilyface Feb 03 '20 at 10:10
  • @smilyface what I want to do is skip the weekends, so when I add 48 hours from Friday, it must be Tuesday. Is it 2 days right? – Darmawan Feb 04 '20 at 00:40

1 Answers1

0

Welcome to stack overflow. Since PHP7 is available on many servers, you can use the comfortable date classes. The following example does exactly what you want. The classes used have been available since PHP5 (for several years).

$start = new DateTime('2020-01-31T23:00:00');
$end = (new DateTime('2020-01-31T23:00:00'))->modify('midnight +3 weekdays');
$interval = new DateInterval('PT1H');

$range = new DatePeriod($start, $interval, $end);
foreach ($range as $date) {
    if ($date->format('N') == 6 || $date->format('N') == 7) {
        continue;
    }
    var_dump($date->format('Y-m-d H:i:s'));
}

First we define a start and an end date. For this example I 've chosen last friday to demonstrate, that saturday and sunday are ignored. Don't let the three days of the week confuse you. We start at midnight on the start date to get the full two workdays including the whole start date. Since you wanted every single hour, we use the DateInterval class and define an hourly interval. With start, end and the interval we can iterate through a range with the DatePeriod class. In the loop we check, if the weekday (assuming that 6 is saturday and 7 is sunday) is saturday and sunday, the date will be modified until the next monday is reached. The output is every hour skipping the weekend.

Please have a look in the php documentation for date and time to understand what these classes can do for you and how they work. Don 't just copy this example.

Marcel
  • 4,854
  • 1
  • 14
  • 24
  • I agree DateTime class could be used. Other than that your answer is utterly ineffective and it doesn't even achieve OP's goal. – slepic Feb 03 '20 at 09:07
  • Yes, true. As the topic starter explained later in the comments, what he exactly wants and it is not done with just adding weekdays, this answer might not be the exact solution. But SO was never ment supplying exact solutions. It gets him in the right direction. Adding hours and ignoring the weekend isn 't that far from the example shown below. So why this is ineffective? That 's the only question I 've got. What would be an effective way archiving the result the starter is asking for? – Marcel Feb 03 '20 at 11:50
  • Because the loop skips every saturday and sunday 24 times instead of once. In other words you are supplying the foreach with at least 24 times more DateTime objects representing saturdays and sundays than necesary. Then ask for property "day of week" which does not differ among those 24 instances. – slepic Feb 03 '20 at 12:46