Edit report at https://bugs.php.net/bug.php?id=62911&edit=1
ID: 62911
Comment by: kairo at kairo dot at
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:
One issue with this not existing is that e.g. array_diff() doesn't work on arrays of DateTime
objects as it tries to compare the string representations of the objects and you end up with
"Object of class DateTime could not be converted to string".
Previous Comments:
------------------------------------------------------------------------
[2019-01-30 10:37:47] info at typoworx dot com
By the way I completly like and agree with the approach done by willfitch@php.net using a
getter/setter method to override the __toString-Format.
The default-format should be anything that contains TimeZone in String-Format, too (like UTC).
------------------------------------------------------------------------
[2019-01-30 10:32:56] info at typoworx dot com
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"
------------------------------------------------------------------------
[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.
------------------------------------------------------------------------
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