Re: Add __toString() to DateInterval

From: Date: Mon, 15 Mar 2021 09:22:56 +0000
Subject: Re: Add __toString() to DateInterval
References: 1 2  Groups: php.internals 
Request: Send a blank email to internals+get-113533@lists.php.net to get a copy of this message
I would assume the moment I call some kind of ISO conversion or serialization method on a dynamic
interval, I’d receive output relative to now. createFromDateString creates the
interval relative to now, so why shouldn’t the reverse hold true?

Regards,
Moritz

> Am 15.03.2021 um 10:18 schrieb Derick Rethans <derick@php.net>:
> 
> On Wed, 3 Mar 2021, Moritz Friedrich wrote:
> 
>> I would like to propose adding a __toString() method to the 
>> DateInterval class that should return a valid ISO8601 interval 
>> (https://en.wikipedia.org/wiki/ISO_8601#Time_intervals). As it stands, 
>> the class supports creating instances from such interval strings 
>> passed to its constructor, but the reverse isn’t true: The only way to 
>> build a string representation of the interval is by querying the 
>> DateInterval::format method for the individual durations multiple 
>> times (see 
>> https://stackoverflow.com/questions/33787039/format-dateinterval-as-iso8601#answers
>> 
>> for examples).
> 
> The DateInterval class can also contain intervals that can't be 
> represented with such an ISO string. For example, how are you going to 
> do:
> 
> $dt = DateInterval::createFromDateString( "next weekday" );
> 
> or
> 
> $dt = DateInterval::createFromDateString( 'next Monday 02:00' );
> 
> __toString needs to work in every case, and I don't think you can do 
> that with ISO 8601 interval strings.
> 
> cheers,
> Derick
> 
> -- 
> PHP 7.4 Release Manager
> Host of PHP Internals News: https://phpinternals.news
> Like Xdebug? Consider supporting me: https://xdebug.org/support
> https://derickrethans.nl | https://xdebug.org | https://dram.io
> twitter: @derickr and @xdebug


Thread (19 messages)

« previous php.internals (#113533) next »