PHP strtotime incrementing by weekdays
Asked Answered
B

1

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".

Brazilein answered 17/4, 2015 at 19:9 Comment(7)
PHP version? Looked at bugs.php.net? "Sunday" is the part that suggests old bug....bugs.php.net/bug.php?id=61642Hewes
PHP Version 5.4.38. My original thought was a timezone issue.Brazilein
Bug #61642 modify("+5 weekdays") returns SundayFury
going off topic... Is there a \DateTime approach now that is preferable? I tend to shun date / strtotime these days.Hewes
@Fury It's not a bug, it's a feature!Refutation
@salathe: Didn't think to check that, but ok... seems like this is (was) a PHP bug. Thanks guys.Brazilein
@Hewes I just tried: $date = new DateTime("2015-04-17"); $date->modify("+5 weekdays"); echo $date->format("Y-m-d"); with the same result...Truss
G
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*

Goldina answered 17/4, 2015 at 19:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.