php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #79442 Modify does not support multiple modifications at once
Submitted: 2020-04-01 07:38 UTC Modified: 2020-04-01 08:39 UTC
From: michael dot vorisek at email dot cz Assigned:
Status: Open Package: Date/time related
PHP Version: 7.4.4 OS:
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please — but make sure to vote on the bug!
Your email address:
MUST BE VALID
Solve the problem:
20 - 3 = ?
Subscribe to this entry?

 
 [2020-04-01 07:38 UTC] 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

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2020-04-01 07:56 UTC] 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:57 UTC] sjon@php.net
-Summary: Nth day of month can not be set +Summary: Modify does not support multiple modifications at once -Type: Bug +Type: Feature/Change Request
 [2020-04-01 08:04 UTC] michael dot vorisek at email dot cz
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
 [2020-04-01 08:39 UTC] derick@php.net
> Is it currently possible to use multiple modifications to a date like "first day of -2 months, +15 days" in one modify() call?

No, that's not possible.

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

I don't think so, but I will check and see whether it's easy to add.

I also see that you're using "clone" here. If instead of \DateTime you use \DateTimeImmutable, that clone isn't needed, and you will have less issues running two ->modify() calls (as you don't need to clone twice).
 
PHP Copyright © 2001-2020 The PHP Group
All rights reserved.
Last updated: Wed Nov 25 09:01:23 2020 UTC