[php-src] Issue #9999: Cannot create a DateInterval with a negative duration

From: Date: Fri, 25 Nov 2022 10:28:51 +0000
Subject: [php-src] Issue #9999: Cannot create a DateInterval with a negative duration
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-242905@lists.php.net to get a copy of this message
Issue: https://github.com/php/php-src/issues/9999
Author: Leslie-G

### Description

Hello, 

I'm not sure if this is a bug or something that need to be implemented, so feel free to change
the label :)

As I was implementing a connectivity between my backoffice and a third-party API where I need to
manage some DateInterval with the data I received, I came across the impossibility to create a
DateInterval with a negative duration.

The data I receive is based on a time unit, the start of the time unit is at midnight and the end of
the time unit is the beginning of the next time unit. 

Here is an example of what is happening in this third-party API and why I receive a negative
duration

![Capture d’écran du 2022-11-25
10-45-50](https://user-images.githubusercontent.com/1783063/203951222-a46fb9cd-f000-4361-a6cb-5058d87df067.png)


For the start offset everything is working as expected because I receive a positive duration
P0M0DT10H0M0S.
But for the end offset I receive a negative duration P0M0DT-4H0M0S so when I try to
create a new DateInterval I get an error.

The example on https://3v4l.org/olhe7

The following code: 

```php
<?php
$interval = new \DateInterval('P0M0DT-4H0M0S');
```

Resulted in this output:
```
Fatal error: Uncaught Exception: Unknown or bad format (P0M0DT-4H0M0S)
```

But I expected this output instead:
```
a new DateInterval
```


### PHP Version

PHP 8.1.12

### Operating System

Ubuntu 22.04.1


Thread (1 message)

  • Leslie-G
« previous php.bugs (#242905) next »