Chronos FormattingTrait

I dug around in the code and found FormattingTrait, it has a method

public function toDateTimeString()
    {
        return $this->format('Y-m-d H:i:s');
    }

But in a view, how do you call this method? I am still trying to figure out date time in cakephp.

Well found this in the docs


Formatting Strings

Chronos provides a number of methods for displaying our outputting datetime objects:

// Uses the format controlled by setToStringFormat()
echo $date;

// Different standard formats
echo $time->toAtomString();      // 1975-12-25T14:15:16-05:00
echo $time->toCookieString();    // Thursday, 25-Dec-1975 14:15:16 EST
echo $time->toIso8601String();   // 1975-12-25T14:15:16-05:00
echo $time->toRfc822String();    // Thu, 25 Dec 75 14:15:16 -0500
echo $time->toRfc850String();    // Thursday, 25-Dec-75 14:15:16 EST
echo $time->toRfc1036String();   // Thu, 25 Dec 75 14:15:16 -0500
echo $time->toRfc1123String();   // Thu, 25 Dec 1975 14:15:16 -0500
echo $time->toRfc2822String();   // Thu, 25 Dec 1975 14:15:16 -0500
echo $time->toRfc3339String();   // 1975-12-25T14:15:16-05:00
echo $time->toRssString();       // Thu, 25 Dec 1975 14:15:16 -0500
echo $time->toW3cString();       // 1975-12-25T14:15:16-05:00

// Get the quarter
echo $time->toQuarter();         // 4;

Apparently a genius decided to leave out this example:

toDateTimeString()

If toDateTimeString() is in the docs anywhere it is well hidden, those docs leave much to be desired.
Also I notice no “use” statement is needed, this seems to work fine:

echo "<td style=\"width:40px;\">" . $row->lastedit->toDateTimeString() . "</td>";

The docs could stand to have better examples for folks new to cakephp. If I had not come from laravel which uses traits, I would have never figured this out. How is a newcomer? Also what is that I18n stuff, I got it formatted with no I18n. So this is also very confusing, the I18n topic.

Wouldn’t cakephp be better off just using php built in date time stuff?

I created an issue since a method was left out of the docs. To me Y-m-d H:i:s is a standard as well since a major database, mysql stores datetime in that format. The issue is #4340

That genius would be me. I also happen to be the genius who updated the docs :smile: I didn’t originally include all the methods as I didn’t forsee people using them all. I was wrong.

@markstory thanks, and the genius thing was just a Cliche. Funny, Laravel just natively works with the mysql formatted date out of the box. I wish cake did also. My rule of thumb is just to use the date format native to the database. If sqlserver I’d do it different.

Wouldn’t cakephp be better off just using php built in date time stuff?

If you want to write unit tests that depend on Date or Time, the fixation feature of Chronos is brilliant - and the reason I used Chronos everywhere in my cake application.

https://book.cakephp.org/3.0/en/chronos.html#testing-aids