Hi!
The way moodle stores dates in the db is weird.
On the Time API wiki it is said that "Moodle always stores all times in unixtime format (number of seconds since epoch) which is independent of timezones.". So I assumed that it stores the date in UTC.
But that doesn't seem to be the case (?). We have a birth date field on our site and we use the MySQL function FROM_UNIXTIME() to get the actual dates in some DB reports. We observed that with some users the birth date is wrong by one day in said reports.
I created a test on two moodle installation (details at the bottom). I added 12 Date/Time profile field without the Include time? option on both installation. Then I filled those up on my profile with dates. Then I ran the following query on both databases:
SET time_zone='UTC';
SELECT data, from_unixtime(data) UTC FROM mdl_user_info_data; -- the table doesn't have any other values
The results are quite strange:
data | UTC |
---|---|
1516834800 | 2018-01-24 23:00:00 |
1519513200 | 2018-02-24 23:00:00 |
1521932400 | 2018-03-24 23:00:00 |
1524607200 | 2018-04-24 22:00:00 |
1527199200 | 2018-05-24 22:00:00 |
1529877600 | 2018-06-24 22:00:00 |
1532469600 | 2018-07-24 22:00:00 |
1535148000 | 2018-08-24 22:00:00 |
1537826400 | 2018-09-24 22:00:00 |
1540418400 | 2018-10-24 22:00:00 |
1543100400 | 2018-11-24 23:00:00 |
1545692400 | 2018-12-24 23:00:00 |
data | UTC |
---|---|
1516838400 | 2018-01-25 00:00:00 |
1519516800 | 2018-02-25 00:00:00 |
1521936000 | 2018-03-25 00:00:00 |
1524610800 | 2018-04-24 23:00:00 |
1527202800 | 2018-05-24 23:00:00 |
1529881200 | 2018-06-24 23:00:00 |
1532473200 | 2018-07-24 23:00:00 |
1535151600 | 2018-08-24 23:00:00 |
1537830000 | 2018-09-24 23:00:00 |
1540422000 | 2018-10-24 23:00:00 |
1543104000 | 2018-11-25 00:00:00 |
1545696000 | 2018-12-25 00:00:00 |
Somehow, daylight savings are affecting this. But why? Shouldn't be the result 2018-12-25 00:00:00 in all cases?
And why are they different between the two systems?
On both installation, running <?php echo time(); at the same time prints the same timestamp.
Installation details:
Moodle | 3.3.5 |
---|---|
OS | Windows 7 x64 |
Apache | 2.4.33 (Win64) |
PHP | 7.0.29 |
DB | MariaDB 10.2.13 |
Moodle | 3.4.4 |
---|---|
OS | Ubuntu 18.04 (host) |
Apache | Apache/2.4.25 (Debian) - from docker image: php:7.1-apache |
PHP | 7.1.19 |
DB | MySQL 5.7.22 |