RedBeanPHP - R::isoDateTime() - How to set timezone?

回眸只為那壹抹淺笑 提交于 2020-01-05 04:19:07

问题


There is a convenience function in RedBeanPHP ORM for ceating dates.

$time = R::isoDateTime();

How can I set a time zone?

The default function does not return the time of the machine on which RB is running on.


回答1:


It appears from reading the source code, that R::isoDateTime() is just a convenience method. It simply calls the PHP time() function, then formats the result as a string using the date function.

I didn't test it, but in theory - the date_default_timezone_set function should work. For example:

date_default_timezone_set('America/Los_Angeles');



回答2:


As Matt pointed out, R::isoDateTime() is just a convenience function.

In all versions of RedBean I've checked (3.x, 4.x), it is defined as:

@date( 'Y-m-d H:i:s', $time )`

i.e.: a timestamp without any timezone information. e.g.: 2017-09-05 18:49:16.


Under MySQL:

You are probably using datetime without any timezone information, and R::isoDateTime() will do just fine.


But pay attention when using Postgres:

Postgres will understand such times, without a timezone, as times in Postgres' timezone (not PHP's!) So even though you set the time using RedBean, you might find out the dates are not what you expected them to be.

This should help:

  • To check Postgres's default timezone: SHOW timezone;.
  • To change the default timezone for a Postgres role (user): ALTER ROLE myuser SET timezone = 'Europe/Paris';.
  • I would suggest setting the default timezone for Postgres to what you want – to be safe. However, it's better practice to indicate the timezone explicitly whenever you work with time. This will avoid having your time interpreted as UTC by accident when you meant for it to be in a specific time zone. So instead of a timezone-less R::isoDateTime() you might want to use something like @date( 'c' ), eg.: 2017-09-05T18:49:16+02:00.

And finally keep in mind Postgres stores everything as UTC, but can/will display its timestamp without a time zone type (AKA timestamptz) in a variety of timezones, based on its currently-set timezone setting. e.g.: 2017-09-05T18:49:16+02:00 if Europe/Paris is set, but 2017-09-05T16:49:16+00:00 if UTC is set (both refer to the same time.)

(I love RedBean but haven't seen this covered anywhere. Hopefully this'll help someone.)



来源:https://stackoverflow.com/questions/43784850/redbeanphp-risodatetime-how-to-set-timezone

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!