Re: Adding weekdays to DateTime resets the time
| From: | David Lundgren | Date: | Mon, 04 May 2015 19:50:52 +0000 |
| Subject: | Re: Adding weekdays to DateTime resets the time | ||
| References: | 1 2 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-86110@lists.php.net to get a copy of this message | ||
There is actually already a bug for about this:
https://bugs.php.net/bug.php?id=54909
I ran into this several months ago when I upgraded a project from 5.3.2
to 5.5 and found that in 5.3.6 the behavior had changed. I ended up
working around it in my code. It was definitely annoying but had we
upgraded PHP versions sooner I wouldn't have had any issue with this in
the time crunch I had.
Our unit tests around that code, while complex, became that much better
to actually account for the finer details of date/time.
- Dave
On 5/4/15 12:49 PM, Stelian Mocanita wrote:
> I think the best approach would be to open a bug under
> https://bugs.php.net/
> since this does
> not look like intended behavior.
>
> Regards,
> Stelian
>
> On Mon, May 4, 2015 at 5:33 PM, Markus Fasselt <markus.fasselt@gmail.com>
> wrote:
>
>> Hi,
>>
>> when I modify a DateTime object and add or subtract weekdays, it happens
>> that the current time is reset to 00:00:00.
>>
>> This effect does not occur when I add days. I also experienced the same
>> behavior with strototime.
>>
>>> <?php
>>>
>>> $a = new DateTime(strtotime('2015-05-04 17:22'));
>>> var_dump((string) $a->format('Y-m-d H:i:s')); // 2015-05-04 17:22:00
>>> $a->modify('+1 day');
>>> var_dump((string) $a->format('Y-m-d H:i:s')); // 2015-05-05 17:22:00
>>> $a->modify('+1 weekday');
>>> var_dump((string) $a->format('Y-m-d H:i:s')); 2015-05-06 00:00:00
>>>
>>> var_dump(date('Y-m-d H:i:s', strtotime('+1 weekday'))); //
>>> 2015-05-05
>> 00:00:00
>>
>> http://3v4l.org/554H4
>>
>> There seems to be slighly different behavior between the different PHP
>> versions. strotime always had this behavior. DateTime::modify() worked
>> correct until PHP 5.3.5.
>>
>> I don't know if this behavior was changed on purpose (I didn't find
>> something on it). If so, why? Otherwise I would suggest to fix this, as
>> it is really confusing.
>>
>> Regards,
>> Markus
>>
>> --
>> PHP Internals - PHP Runtime Development Mailing List
>> To unsubscribe, visit: http://www.php.net/unsub.php
>>
>