# |
Jun 20th 2021, 17:13 |
amayer |
Thanks for pointing me down the right path @ndm! |
# |
Jun 20th 2021, 17:12 |
amayer |
Looks like `$config = ConnectionManager::getConfig('default'));` is what I was looking for! |
# |
Jun 20th 2021, 17:00 |
amayer |
I'll give the ConnectionManager thing a try. |
# |
Jun 20th 2021, 16:59 |
ndm |
well there you go :) |
# |
Jun 20th 2021, 16:59 |
ndm |
Some configuration is "consumed", meaning it is being removed from the config after reading, and then it's only available via the consumers, in your case the connections. Try `\Cake\DataSource\ConnectionManager::get('default')->getConfig()` |
# |
Jun 20th 2021, 16:59 |
amayer |
I may have found the issue... Looks like `Datasources` is being consumed in config/bootstrap.php |
# |
Jun 20th 2021, 16:50 |
amayer |
Hello, I am writing a plugin (composer package) and would like to retrieve the `Datasources.default` settings using `\Cake\Core\Configure::read()`, but it doesn't seem to have a value. All other settings in my config/app_local.php (main application, cakephp 4.2.7) seem to be there. Is there a reason I can't access `Datasources.default` from a plugin? |
# |
Jun 20th 2021, 13:14 |
ndm |
As you've done it in your snippet would be one way, it should work fine for date/time objects that are being casted to string. But as mentioned, your output looks like the default JSON encode format, the default to string format is i18n aware, and will never give you the format that's shown in your output. |
# |
Jun 20th 2021, 13:12 |
alamnaryab |
I need one format that is `yyyy-MM-dd HH:mm:ss` for any Language/Locale all the users are in same timezone, where and how can I set? |
# |
Jun 20th 2021, 13:10 |
ndm |
Indeed, it depends. If he'd need all output in the same format, then setting a default to string format could work just fine. But that's different from timezone conversions, the to string logic isn't aware of timezones, only of formats. All that being said, his snippet should have worked fine, so there must be something else to it. That example output looks like the default JSON encode format. |
# |
Jun 20th 2021, 13:02 |
kevin.pfeifer |
I was just thinking in the example of @alamnaryab where he just wants to show a different datetime format for a different language prefix. Instead of having to adjust all outputted datetime fields in the template there should be somse sort of global default which can be overwritten by e.g. a language prefix. sure it is all very customizable but as you said it all depends on the usecase ^^ |
# |
Jun 20th 2021, 12:56 |
ndm |
If you'd have dates with different timezones, you'd have to store that timezone in the DB alongside, and you'd have to come up with something on your own, a custom helper would be easy enough. |
# |
Jun 20th 2021, 12:55 |
ndm |
Yeah, it's called `outputTimezone` |
# |
Jun 20th 2021, 12:55 |
ndm |
Unfortunately proper documentation for this is lacking... it's a somewhat complicated topic, so that's not too surprising. It really depends on the requirements of your application, if for example all dates should be converted to the same timezone, the time helper has a config option for that IIRC. |
# |
Jun 20th 2021, 12:51 |
kevin.pfeifer |
so datetime fields for e.g. created and modified should be saved in UTC (therefore looking into the database via sql cli or phpmyadmin should show UTC values). But where in cakephp (in a multilingual setup) do I have to add the logic to show different formats and/or timezones? Is there a guide somewhere? |
# |
Jun 20th 2021, 12:46 |
slackebot1 |
required, even if it's more work. I never not ran into a hot steaming pile of trouble when doing timezone conversions for saving/reading. |
# |
Jun 20th 2021, 12:46 |
slackebot1 |
imagine that this can make things extra annoying :) `TIMESTAMP` is converted from server/connection timezone to UTC on save, and from UTC to server/connection timezone on read (this is what the datasource config timezone would be used for). As always when this topic comes up, I'd like to mention that from my personal experience, I can only suggest to go UTC all the way, and convert to specific timezones on PHP/frontend level when |
# |
Jun 20th 2021, 12:46 |
ndm |
@kevin.pfeifer `DATETIME` columns can hold a timezone offset (not a timezone name) as of MySQL 8. CakePHP accepts such formats even for the default `DateTimeType` class, but it won't save it like that, only the `DateTimeTimezoneType` does, which however isn't automatically mapped. Also MySQL will not return the offset when doing a select, instead it will return the date with the offset subtracted. I haven't tried it yet, but I could |
# |
Jun 20th 2021, 10:36 |
kevin.pfeifer |
i guess i am mixing up the `datetime` mysql field with the `timestamp` field timestamp is always utc, datetime can "save" timezones |
# |
Jun 20th 2021, 10:31 |
kevin.pfeifer |
As far as I know there are • the timezone the data is saved in the database (in a datetime column, or is this always UTC?) • the timezone setting present in the datasource config (which should be the same as the data saved in the database as far as I know) • the `setToStringFormat` from above • and there are view helpers as well to change the format for php datetime objects |
# |
Jun 20th 2021, 10:20 |
dereuromark |
localized output vs default (code/iso) output |
# |
Jun 20th 2021, 10:20 |
kevin.pfeifer |
Guess I have to go a bit deeper here to fully understand where date formats (and maybe timezones as well( are present and can/should be changed |
# |
Jun 20th 2021, 10:18 |
kevin.pfeifer |
I am sorry but what do you mean by "both" here? |
# |
Jun 20th 2021, 10:14 |
dereuromark |
at least when local date format != code one |
# |
Jun 20th 2021, 10:13 |
dereuromark |
because you are using it for both (which works, but is maybe not the best idea) |
# |
Jun 20th 2021, 10:10 |
kevin.pfeifer |
but then why does the `setToStringFormat` work for me without that helper? |
# |
Jun 20th 2021, 09:43 |
dereuromark |
most bake templates have that properly built in, though, https://github.com/dereuromark/cakephp-setup/blob/master/templates/bake/Template/view.twig#L95 etc |
# |
Jun 20th 2021, 09:42 |
dereuromark |
the string casting default format is mainly for code output (e.g. in json formatting) |
# |
Jun 20th 2021, 09:39 |
dereuromark |
if you want to format it, you should use Time helper here |
# |
Jun 20th 2021, 04:45 |
alamnaryab |
In view `<td><?= h($entityRow->created) ?></td>` |
# |
Jun 19th 2021, 23:59 |
greg138 |
They are being formatted incorrectly in your views? If so, please show an example of how you are outputting it. Or it's formatted incorrectly in the data (e.g. entities) you're loading from the database? |
# |
Jun 19th 2021, 21:18 |
kevin.pfeifer |
well one thing I can tell you is the fact, that your datetime output above is ISO8601 (which I guess is the default format being outputted by cakephp) |
# |
Jun 19th 2021, 21:04 |
alamnaryab |
yes, it is default ar, I have multilangual site but datetime I need in `yyy-MM-dd HH:mm:ss` format from database fields |
# |
Jun 19th 2021, 20:58 |
kevin.pfeifer |
have you set your `App.defaultLocale` to something special in your `config/app_local.php` (or the `config/.env` file if you are using it) |
# |
Jun 19th 2021, 20:52 |
alamnaryab |
yes |
# |
Jun 19th 2021, 20:50 |
kevin.pfeifer |
so like the outputted `$entity->created` datetime in your frontend didn’t change? |
# |
Jun 19th 2021, 20:46 |
alamnaryab |
I wrote this at end of bootstrap.php ``` Time::setToStringFormat('HH:mm:ss'); FrozenTime::setToStringFormat('HH:mm:ss'); Date::setToStringFormat('yyyy-MM-dd HH:mm:ss'); FrozenDate::setToStringFormat('yyyy-MM-dd HH:mm:ss'); ``` but still date from database is showing like `2015-09-26T18:58:40+04:00` |
# |
Jun 19th 2021, 20:30 |
kevin.pfeifer |
but be aware if you want to set the date format yourself (not using the preconfigured constants) with something like `'Y-m-d H:i:s'` because these “letters” are not your typically PHP date variables. Instead this is used here: http://userguide.icu-project.org/formatparse/datetime |
# |
Jun 19th 2021, 20:25 |
kevin.pfeifer |
this should be added to your `config/bootstrap.php` at the bottom |
# |
Jun 19th 2021, 20:23 |
kevin.pfeifer |
https://book.cakephp.org/4/en/core-libraries/time.html#setting-the-default-locale-and-format-string |
# |
Jun 19th 2021, 20:15 |
alamnaryab |
Using Cakephp 4 what is better way to change dateformat, datetimeformat at project level? |