Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:113336 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 11759 invoked from network); 3 Mar 2021 10:47:19 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 3 Mar 2021 10:47:19 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 9B8111804DC for ; Wed, 3 Mar 2021 02:37:48 -0800 (PST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=1.0 required=5.0 tests=BAYES_50,DKIM_INVALID, DKIM_SIGNED,SPF_HELO_PASS,SPF_PASS autolearn=no autolearn_force=no version=3.4.2 X-Spam-Virus: No X-Envelope-From: Received: from mail.hades.9dev.de (mail.hades.9dev.de [88.99.187.36]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Wed, 3 Mar 2021 02:37:47 -0800 (PST) Received: from [127.0.0.1] (localhost [127.0.0.1]) by localhost (Mailerdaemon) with ESMTPSA id DAF315E007 for ; Wed, 3 Mar 2021 11:37:43 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=9dev.de; s=dkim; t=1614767864; h=from:subject:date:message-id:to:mime-version:content-type: content-transfer-encoding; bh=34Sh9G+u1Vxan2Od5PTQqjOkDag/s2xsFPtEr7o64V8=; b=j9eXCL4oO4oZhZe7s1nd/evC1Rp1raOZB2US3AWlXqRFXvwQli0z0rdGyhTz+7bsLGCZRF oRG9t7ZylURZ6Fvcivb+aExYeybDBXttPEKmqfO1Nvb4JnPb7W/p0SRIgrXh6JF4TlJjFp CGlT1O73s7c1NjSdkjSMHVGgZekl0UFLGWM+nTiHSjjcapluG7GztmY8mOPrdVgVItXG6b Mbv85z3sY5HNtEKZmcnPRB2wHKB1hTgheIbPfxVkT5x9mrTfUiWp3ZAc129rqaej6Iit1V uSzcQTPdh22NjesTFFPwPFjhwqLPQyrxJlAi+3HJ0JbuHjeBoodR2NbbNGQ1UQ== Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Mime-Version: 1.0 (Mac OS X Mail 14.0 \(3654.60.0.2.21\)) Message-ID: Date: Wed, 3 Mar 2021 11:37:42 +0100 To: internals@lists.php.net X-Last-TLS-Session-Version: TLSv1.2 Subject: Add __toString() to DateInterval From: m@9dev.de (Moritz Friedrich) Hi internals, I=E2=80=99ve been active in the PHP ecosystem as @Radiergummi for quite = a while now, but not on internals yet, so: nice to meet you all! 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=E2=80=99t 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-iso860= 1#answers for examples). Allowing to cast `DateInterval`s to strings would make the most sense in = my opinion, and probably doesn=E2=80=99t break BC. Use cases include = communicating with other APIs that work with ISO8601, or persistence of = periods in databases, for example.=20 So instead of the following: function formatDateInterval(DateInterval $interval, string $default = =3D 'PT0S'): string { return rtrim(str_replace( ['M0S', 'H0M', 'DT0H', 'M0D', 'P0Y', 'Y0M', 'P0M'], ['M', 'H', 'DT', 'M', 'P', 'Y', 'P'], $interval->format('P%yY%mM%dDT%hH%iM%sS') ), 'PT') ?: $default; } =20 formatDateInterval(new DateInterval('P1DT5H')) =3D=3D=3D 'P1DT5H' I=E2=80=99d like to be able to do this: (string)new DateInterval('P1DT5H') =3D=3D=3D 'P1DT5H' Following alternative approaches come to mind: 1. Adding a new, specialized method to the class (eg. = `DateInterval::toIsoString(): string`): =20 While being the most BC-safe option, this would be completely = different to the rest of the PHP date APIs. 2. Adding a new interval format constant to be passed to = `DateInterval::format` (eg. `DATE_INTERVAL_ISO8601`): =20 Most in line with the `DateTime` API, but probably requires special = case handling in `format` code. =46rom those, having `__toString` seems most desirable to me. =20 While I=E2=80=99m not proficient with C, I=E2=80=99d really like to see = this implemented and would gladly help out where I can. Might someone be = able to advise? Sincerely Moritz / Radiergummi=