php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #63521 Adding 5 weekdays to Fri, Sat or Sun results in Sunday
Submitted: 2012-11-14 19:00 UTC Modified: 2016-08-07 09:45 UTC
Votes:23
Avg. Score:4.2 ± 1.0
Reproduced:21 of 21 (100.0%)
Same Version:5 (23.8%)
Same OS:11 (52.4%)
From: linesarefuzzy at gmail dot com Assigned: cmb (profile)
Status: Closed Package: Date/time related
PHP Version: 5.3.18 OS: Mac, Linux
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: linesarefuzzy at gmail dot com
New email:
PHP Version: OS:

 

 [2012-11-14 19:00 UTC] linesarefuzzy at gmail dot com
Description:
------------
Adding 5 weekdays to a Friday, Saturday or Sunday returns the following Sunday 
instead of Friday. Same issue when adding multiples of 5. Starting with any other 
weekday works as expected, as does adding any other number of weekdays. 

Test script:
---------------
echo date ( 'l, F j, Y', strtotime ( 'friday + 1 weekdays' ) ) . "\n";
echo date ( 'l, F j, Y', strtotime ( 'friday + 2 weekdays' ) ) . "\n";
echo date ( 'l, F j, Y', strtotime ( 'friday + 3 weekdays' ) ) . "\n";
echo date ( 'l, F j, Y', strtotime ( 'friday + 4 weekdays' ) ) . "\n";
echo date ( 'l, F j, Y', strtotime ( 'friday + 5 weekdays' ) ) . "\n";
echo "\n";             
echo date ( 'l, F j, Y', strtotime ( 'saturday + 1 weekdays' ) ) . "\n";
echo date ( 'l, F j, Y', strtotime ( 'saturday + 2 weekdays' ) ) . "\n";
echo date ( 'l, F j, Y', strtotime ( 'saturday + 3 weekdays' ) ) . "\n";
echo date ( 'l, F j, Y', strtotime ( 'saturday + 4 weekdays' ) ) . "\n";
echo date ( 'l, F j, Y', strtotime ( 'saturday + 5 weekdays' ) ) . "\n";
echo "\n";             
echo date ( 'l, F j, Y', strtotime ( 'sunday + 1 weekdays' ) ) . "\n";
echo date ( 'l, F j, Y', strtotime ( 'sunday + 2 weekdays' ) ) . "\n";
echo date ( 'l, F j, Y', strtotime ( 'sunday + 3 weekdays' ) ) . "\n";
echo date ( 'l, F j, Y', strtotime ( 'sunday + 4 weekdays' ) ) . "\n";
echo date ( 'l, F j, Y', strtotime ( 'sunday + 5 weekdays' ) ) . "\n";

Expected result:
----------------
Monday, November 19, 2012
Tuesday, November 20, 2012
Wednesday, November 21, 2012
Thursday, November 22, 2012
Friday, November 23, 2012

Monday, November 19, 2012
Tuesday, November 20, 2012
Wednesday, November 21, 2012
Thursday, November 22, 2012
Friday, November 23, 2012

Monday, November 19, 2012
Tuesday, November 20, 2012
Wednesday, November 21, 2012
Thursday, November 22, 2012
Friday, November 23, 2012

Actual result:
--------------
Monday, November 19, 2012
Tuesday, November 20, 2012
Wednesday, November 21, 2012
Thursday, November 22, 2012
Sunday, November 25, 2012

Monday, November 19, 2012
Tuesday, November 20, 2012
Wednesday, November 21, 2012
Thursday, November 22, 2012
Sunday, November 25, 2012

Monday, November 19, 2012
Tuesday, November 20, 2012
Wednesday, November 21, 2012
Thursday, November 22, 2012
Sunday, November 25, 2012

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2012-11-22 08:33 UTC] jon dot drummond at barrowsonline dot com
Has anyone found a solution to this yet? We can not wait for a patch & need a 
workaround in the mean time.
 [2012-11-23 02:58 UTC] 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');
?>
 [2012-12-28 10:42 UTC] 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
 [2013-03-02 14:04 UTC] 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.
 [2013-04-19 06:41 UTC] 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)
 [2014-08-22 17:42 UTC] 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.
 [2014-10-10 10:54 UTC] den4b at mail dot ru
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;
}
 [2016-08-07 09:45 UTC] cmb@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: cmb
 [2016-08-07 09:45 UTC] cmb@php.net
Indeed, this bug has been fixed as of PHP 5.5.0, see
<https://3v4l.org/ED95p>.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun Dec 01 17:01:32 2024 UTC