Bug #63521 [Com]: Adding 5 weekdays to Fri, Sat or Sun results in Sunday

From: Date: Fri, 10 Oct 2014 10:54:58 +0000
Subject: Bug #63521 [Com]: Adding 5 weekdays to Fri, Sat or Sun results in Sunday
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-188004@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=63521&edit=1

 ID:                 63521
 Comment by:         den4b at mail dot ru
 Reported by:        linesarefuzzy at gmail dot com
 Summary:            Adding 5 weekdays to Fri, Sat or Sun results in
                     Sunday
 Status:             Open
 Type:               Bug
 Package:            Date/time related
 Operating System:   Mac, Linux
 PHP Version:        5.3.18
 Block user comment: N
 Private report:     N

 New Comment:

Here is a simple workaround function for adding and subtracting weekdays:

function addWeekDays(DateTime $date, $numDays)
{
	$numDays = (int)($numDays); // ensure integer!
	$interval = new DateInterval('P1D'); // 1 day
	while ($numDays != 0)
	{
		if ($numDays > 0)
			$date->add($interval);
		else
			$date->sub($interval);
		$dayOfWeek = $date->format('N'); // 1 (Monday) .. 7 (Sunday)
		if (($dayOfWeek >= 1) && ($dayOfWeek <= 5))
		{
			if ($numDays > 0)
				$numDays--;
			else
				$numDays++;
		}
	}
	return $date;
}


Previous Comments:
------------------------------------------------------------------------
[2014-08-22 17:42:08] nabil at thinkshirt dot com

I am still having this issue. +10 and +5 give me the following Sunday instead of Friday on PHP
5.4.29.

------------------------------------------------------------------------
[2013-04-19 06:41:21] ravgu10 at gmail dot com

There is one more issue.
date('Y-m-d',strtotime(date('Y-m-d', strtotime('2013-04-22' . "
+5 Weekday")))); 
Expected result: 2013-04-29 (Monday)
Actual result:2013-04-29 (Monday)
fine but when u do,
date('Y-m-d',strtotime(date('Y-m-d', strtotime('2013-04-29' . "
-5 Weekday"))));
Expected result: 2013-04-22 (Monday)
Actual result:2013-04-20 (Saturday)

------------------------------------------------------------------------
[2013-03-02 14:04:48] payden at paydensutherland dot com

This has been fixed (some time ago, Jul 29, in git).  It's currently in the 
latest 5.5-dev.  It's not currently in latest 5.4, but I guess it should probably 
make it there sometime.

------------------------------------------------------------------------
[2012-12-28 10:42:37] michal dot kocztorz at polcode dot pl

Seems to affect DateTime::modify too.
$dt = new DateTime("2013-01-04"); echo $dt->modify("+5
weekdays")->format("Y-m-d 
H:i:s l") . "<br/>";
$dt = new DateTime("2013-01-05"); echo $dt->modify("+5
weekdays")->format("Y-m-d 
H:i:s l") . "<br/>";
$dt = new DateTime("2013-01-06"); echo $dt->modify("+5
weekdays")->format("Y-m-d 
H:i:s l") . "<br/>";
$dt = new DateTime("2013-01-07"); echo $dt->modify("+5
weekdays")->format("Y-m-d 
H:i:s l") . "<br/>";

Got:
2013-01-13 00:00:00 Sunday
2013-01-13 00:00:00 Sunday
2013-01-13 00:00:00 Sunday
2013-01-14 00:00:00 Monday

------------------------------------------------------------------------
[2012-11-23 02:58:58] linesarefuzzy at gmail dot com

Jon--

Since the bug always results in Sunday when it should be Friday, an
obvious workaround would be to test the weekday of the result, and
if it's Sunday, subtract two days. For example:

<?php
if (date('l', $timestamp) == 'Sunday')
  $timestamp = strtotime(date('Y-m-d', $timestamp) . ' - 2 days');
?>

------------------------------------------------------------------------


The remainder of the comments for this report are too long. To view
the rest of the comments, please view the bug report online at

    https://bugs.php.net/bug.php?id=63521


--
Edit this bug report at https://bugs.php.net/bug.php?id=63521&edit=1


Thread (9 messages)

« previous php.bugs (#188004) next »