jxmot / markdown-hitcounter

A simple, yet silent hit counter intended for Markdown files. Its intended use is in README.md files.
MIT License
0 stars 0 forks source link

Fix date/time #6

Closed jxmot closed 3 years ago

jxmot commented 3 years ago

The date/time on shown in report.html is behind by 5 hours.

But when time is read from the counter and converted to strings then the time is behind by 5 hours.

Possible Solution:

In mdcount.php -

Elsewhere? -

jxmot commented 3 years ago

Chosen Solution:

In mdcount.php -

The actual issue of the time being off was caused by how the time zone was being set. Although it did not cause any PHP errors or warnings the code was incorrect.

Correct Method:

$tm = $data->time = time();
$dt = new DateTime("@$tm");
$tz = new DateTimeZone(tzone());
$dt->setTimezone($tz);
$data->dtime = array($dt->format('Y-m-d'), $dt->format('H:i:s'));

Wrong Method:

$tm = $data->time = time();
$dt = new DateTime("@$tm", new DateTimeZone(tzone()));
$data->dtime = array($dt->format('Y-m-d'), $dt->format('H:i:s'));

According to the PHP manual setting the time zone in the contstructor is only valid with 'now' in the first argument.