php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #62911 Add DateTime::__toString()
Submitted: 2012-08-23 18:15 UTC Modified: 2012-12-18 21:19 UTC
Votes:9
Avg. Score:3.8 ± 1.3
Reproduced:8 of 8 (100.0%)
Same Version:2 (25.0%)
Same OS:2 (25.0%)
From: bdurand-php at ssji dot net Assigned: willfitch (profile)
Status: Wont fix Package: Date/time related
PHP Version: Irrelevant OS:
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please — but make sure to vote on the bug!
Your email address:
MUST BE VALID
Solve the problem:
28 - 13 = ?
Subscribe to this entry?

 
 [2012-08-23 18:15 UTC] bdurand-php at ssji dot net
Description:
------------
Hello,

I want to have a default behavior to convert an instance of the class DateTime into a string. I mean implement the method __toString() in the DateTime class.

The default format should be ISO8601 IMHO, but we can add an attribut to define the default format to use for __toString() and format() method.

Greetings.

Test script:
---------------
<?php
$datetime = new DateTime();
echo $datetime;

Expected result:
----------------
2012-08-23T20:11:21+0200

Actual result:
--------------
PHP Catchable fatal error:  Object of class DateTime could not be converted to string in php shell code on line 1

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2012-08-27 15:09 UTC] willfitch@php.net
-Assigned To: +Assigned To: willfitch
 [2012-08-27 15:15 UTC] willfitch@php.net
This is a good idea.  I will add this, but will also add an RFC as I'd like the 
input of other internals on the output format.
 [2012-08-27 15:17 UTC] 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.
 [2012-08-27 15:17 UTC] derick@php.net
-Status: Assigned +Status: Wont fix
 [2012-08-27 15:24 UTC] 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 17:27 UTC] 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 17:50 UTC] 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-09-01 10:18 UTC] willfitch@php.net
I have reopened this as I've introduced an RFC here: 
https://wiki.php.net/rfc/datetime_tostring.
 [2012-09-01 10:18 UTC] willfitch@php.net
-Status: Wont fix +Status: Re-Opened
 [2012-12-18 21:19 UTC] willfitch@php.net
-Status: Re-Opened +Status: Wont fix
 [2019-01-30 10:32 UTC] 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"
 [2019-01-30 10:37 UTC] 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).
 [2020-06-13 21:46 UTC] kairo at kairo dot at
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".
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Mar 29 01:01:28 2024 UTC