Req #79442 [Com]: Modify does not support multiple modifications at once

From: Date: Wed, 01 Apr 2020 08:04:42 +0000
Subject: Req #79442 [Com]: Modify does not support multiple modifications at once
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-226383@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=79442&edit=1

 ID:                 79442
 Comment by:         michael dot vorisek at email dot cz
 Reported by:        michael dot vorisek at email dot cz
 Summary:            Modify does not support multiple modifications at
                     once
 Status:             Open
 Type:               Feature/Change Request
 Package:            Date/time related
 PHP Version:        7.4.4
 Block user comment: N
 Private report:     N

 New Comment:

We are using modify string from external source where we have one field for it. Basically we do
$date->modify($modString)->format().

If my use case is not supported, I can explode the modify string and call modify() multiple times,
but if there is currently a way to write "15th day of -2 months" please let me know.

"first day of" works, so to be consistent "nth day of" should work too


Previous Comments:
------------------------------------------------------------------------
[2020-04-01 07:56:31] sjon@php.net

what's wrong with two modify calls? It will allow you to do as many modifications as you want,
I don't see how this qualifies as a bug. This works fine:

$d = new \DateTime('2020-03-28 12:00:00');
echo $d ->modify('first day of -2 months')
        ->modify('+15 days')
        ->format('j.n.Y');

------------------------------------------------------------------------
[2020-04-01 07:38:07] michael dot vorisek at email dot cz

Description:
------------
Is it currently possible to use multiple modifications to a date like "first day of -2 months,
+15 days" in one modify() call?

Is it currently possible to to write one modify for "15th day of -2 months"?

Test script:
---------------
$d = new \DateTime('2020-03-28 12:00:00');

// first day of the month, ok
echo ((clone $d)->modify('last day of -2 months')->format('j.n.Y')) .
"\n";

// 15th day of the month - not working
echo ((clone $d)->modify('first day of -2 months, +15
days')->format('j.n.Y')) . "\n"; // bad result
echo ((clone $d)->modify('15th day of -2 months')->format('j.n.Y')) .
"\n"; // not parseable modify string

Expected result:
----------------
31.1.2020
15.1.2020
15.1.2020

Actual result:
--------------
31.1.2020
31.1.2020
Warning: DateTime::modify(): Failed to parse time string (15th day of -2 months) at position 0 (1)
Fatal error: Uncaught Error: Call to a member function format() on bool


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



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


Thread (6 messages)

« previous php.bugs (#226383) next »