11

Today is Friday, April 17, 2015. In my app, it automatically generated a "due date" for each assignment. It's set to "5 business days". To accomplish this, We use:

date('m/d/Y', strtotime("+5 weekdays"));

However, today, this output "04/26/2015". Why? That's a sunday. Why doesn't it give me the 24th, which is what I want?

DEMO: http://codepad.org/2wvnypOC

P.S. After speaking to my boss, we switched to strtotime("+5 days"), but I'm still curious what was wrong with "weekdays".

Anil
  • 21,730
  • 9
  • 73
  • 100
gen_Eric
  • 223,194
  • 41
  • 299
  • 337
  • 1
    PHP version? Looked at bugs.php.net? "Sunday" is the part that suggests old bug....https://bugs.php.net/bug.php?id=61642 – ficuscr Apr 17 '15 at 19:14
  • 1
    PHP Version 5.4.38. My original thought was a timezone issue. – gen_Eric Apr 17 '15 at 19:14
  • 7
    [Bug #61642 modify("+5 weekdays") returns Sunday](https://bugs.php.net/bug.php?id=61642) – salathe Apr 17 '15 at 19:15
  • 1
    going off topic... Is there a `\DateTime` approach now that is preferable? I tend to shun `date` / `strtotime` these days. – ficuscr Apr 17 '15 at 19:17
  • 3
    @salathe [It's not a bug, it's a feature!](https://pbs.twimg.com/media/BrnfdXpIMAAAyTn.jpg:large) – Rizier123 Apr 17 '15 at 19:20
  • 1
    @salathe: Didn't think to check that, but ok... seems like this is (was) a PHP bug. Thanks guys. – gen_Eric Apr 17 '15 at 19:21
  • 2
    @ficuscr I just tried: `$date = new DateTime("2015-04-17"); $date->modify("+5 weekdays"); echo $date->format("Y-m-d");` with the same result... – War10ck Apr 17 '15 at 19:24

1 Answers1

6

It's a bug.

It has been fixed in >= 5.5.0.

So you'll need to work around it or upgrade your php version.

<?php
$today = strtotime('2015-04-17 00:00:00');

echo date('m/d/Y', strtotime("+5 weekdays", $today));
echo "\n";
echo phpversion( );
?>

Working in 5.6*

deW1
  • 5,562
  • 10
  • 38
  • 54