0

I store all datetime values as unix timestamp. Registered users can set personal GMT locale in their profile. What should I do to display all datetime on website in user's GMT locale?

Thank you

Kirzilla
  • 16,368
  • 26
  • 84
  • 129

2 Answers2

1

There's no such thing as "user's GMT locale". You must be referring to the user's timezone.

You can convert unix timestamps to dates in the user timezone this way:

$timestamp = ...;
$tz = new DateTimezone("Europe/Lisbon"); //substitute by the user's timezone
$d = new DateTime("@$timestamp");
$d->setTimezone($d);
echo $d->format(DateTime::RFC822);

If you only have a GMT offset, you can use:

$tz = new DateTimezone("Etc/GMT-12");

Note, however, that if you use GMT offsets, you will have to change them when the users enter or leave daylight saving time.

Artefacto
  • 96,375
  • 17
  • 202
  • 225
-1

With PHP:

date("Y-m-d H:i:s", $timestamp);

With MySQL:

FROM_UNIXTIME()
Amirouche Douda
  • 1,564
  • 1
  • 21
  • 30