Req #62911 [Com]: Add DateTime::__toString()

From: Date: Wed, 30 Jan 2019 10:32:56 +0000
Subject: Req #62911 [Com]: Add DateTime::__toString()
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-219289@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=62911&edit=1

 ID:                 62911
 Comment by:         info at typoworx dot com
 Reported by:        bdurand-php at ssji dot net
 Summary:            Add DateTime::__toString()
 Status:             Wont fix
 Type:               Feature/Change Request
 Package:            Date/time related
 PHP Version:        Irrelevant
 Assigned To:        willfitch
 Block user comment: N
 Private report:     N

 New Comment:

derick@php.net stated:

> It's perfectly possible to subclass DateTime yourself and add a __toString()
> that returns the datetime in your preferred format though.

Indeed that is impossible! I've tried this and PHP / DateTime is ignoring my correctly
implemented __toString-Method in my extended DateTime-Class and throws:

"PHP Catchable Fatal Error: Object of class DateTime could not be converted to string"


Previous Comments:
------------------------------------------------------------------------
[2012-09-01 10:18:12] willfitch@php.net

I have reopened this as I've introduced an RFC here: 
https://wiki.php.net/rfc/datetime_tostring.

------------------------------------------------------------------------
[2012-08-27 17:50:28] willfitch@php.net

It's not a matter of whether PHP can or not; it's a matter of gathering a 
consensus among developers on what standard *should* be default.  I personally 
agree with adding __tostring, but this needs to be proposed in an RFC, which I 
will do tonight.

------------------------------------------------------------------------
[2012-08-27 17:27:15] bdurand-php at ssji dot net

The timezone can be defined in the constructor, with the directive tz php.ini, or with
date_default_timezone_get() function.

A DateTime is a string in fact, so it's logic to have a string cast available. Which format is
more standard than ISO format? Another common format is the timestamp, but it can not represent all
dates.
Other languages did this behavior ​​like python. Why PHP can't?

I know that I can subclass any (core) class... it's not the problem here.

------------------------------------------------------------------------
[2012-08-27 15:24:45] willfitch@php.net

Would it be worth investing time in adding a setter/constructor option for a 
default format type? Setting no default could fallback to one of the constants 
(which still doesn't resolve the non-consensus issue).

------------------------------------------------------------------------
[2012-08-27 15:17:38] derick@php.net

We have had this a few times now, and we should not do this. The main reason is that we will never
be able to come to a consensus on which format to show. It certainly shouldn't have anything
that destroys the encoded timezone identifiers.

It's perfectly possible to subclass DateTime yourself and add a __toString() that returns the
datetime in your preferred format though.

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


The remainder of the comments for this report are too long. To view
the rest of the comments, please view the bug report online at

    https://bugs.php.net/bug.php?id=62911


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


Thread (11 messages)

« previous php.bugs (#219289) next »