1

I store timestamps on my server using a simple timestamp in SQL. When I pull down that timestamp, I run it through the following function in order to format the time.

How do I add to the following function in order to convert $timestamp into whatever timezone the user is querying from?

// Returns the formatted time
function displayDate($timestamp)
{
    $secAgo = time() - $timestamp;
    // 1 day
    if ($secAgo < 86400)
        return date('h:i:A', $timestamp);
    // 1 week
    if ($secAgo < (86400 * 7))
       return date('l', $timestamp);
    // older than 1 week
    return date('m/t/y', $timestamp);
}

2 Answers 2

3

If you have timezone name to change, you can use something like:

$date = new DateTime(date('Y-m-d H:i:s', $timestamp), new DateTimeZone('UTC'));
$date->setTimezone(new DateTimeZone('Asia/Vladivostok'));
return $date->format('m/t/y');

Where Asia/Vladivostok is the user's custom timezone.

Sign up to request clarification or add additional context in comments.

3 Comments

@Walker: there is no any. You have to pass it from javascript (using ajax, for example) or try to guess the TZ using user's IP (worse solution).
Guessing is dangerous; prompt the user instead. It's pretty easy to do this, with a little help from Date.getTimezoneOffset to narrow down the likely guesses.
What do you think about passing the timezoneOffset (in seconds) from javascript to PHP then using that? Is that a viable option?
1

@zerkms's method is the best way to convert timestamps to any timezone.

But if you do that every time you need to display a time, it may have a significant performance hit because you're setting up and tearing down a timezone object every time. There's a convenient shortcut, if all the timestamps on the page are going to be in the same timezone. (Which is usually the case, because users don't change timezones in the middle of an HTTP request.)

Somewhere outside of the function, do:

date_default_timezone_set('America/New_York');  // or any other timezone

It's probably a good idea to tie this into one of your session management functions, so that the same user always gets the same timezone.

Once you do this, date() will automatically start using the correct timezone, assuming that $timestamp represents a Unix timestamp. It will also automatically correct for daylight saving time. So there's no need to change anything in that function.

Use timezone_identifiers_list() to retrieve a list of valid timezones.

3 Comments

I woudn't say that it can cause significant performance issue (since 10k instantiations took ~0.02s on my slow office PC), but indeed it makes code more clear and more supportable.
@zerkms Hmm, the last time I tried to set up a few hundred timezone objects (one for each available timezone), it took a rather noticeable fraction of a second on a slow VM. Maybe it's faster when you reuse the same timezone over and over again?
may be. Also note that I performed the test on windows under apache, which is dramatically slower than on linux.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.