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

2

1

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.

Fustanella answered 4/5, 2017 at 13:54 Comment(0)
H
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');
Hallah answered 4/5, 2017 at 23:54 Comment(1)
Yes. I used date_default_timezone_setFustanella
D
0

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.)

Durham answered 5/9, 2017 at 17:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.