Managing Times for Users in PHP

Sarfraz Ahmed    May 05, 2015 01:31 PM

Most of the time, applications we write are meant to be used by users all over the world from different parts of the world having different time zones. Imagine a users from USA posts an status update and some other user from Asia sees completely different time even though original users might have posted status update moments ago.

Well the easiest way to deal with this problem would be to allow users in your application choose their timezones; for example you can present them with a dropdown of all timezones. That's first step.

Now once you know user's timezone, we can easily convert it back and forth. We can use GMT for this purpose because it makes it easy to convert users' timezones. So when saving times to database for example, we would first convert it to GMT. When reading from database and to show on actual webpage, we convert it from GMT to user's actual timezone. This way, we always save times in GMT and show them back to users according to their timezones.

I have made this simple class you can use in your applications:

/**
 * A class to ease timezone managment for users in an applications
 *
 * @author Sarfraz Ahmed <sarfraznawaz2005@gmail.com>
 * @license http://www.opensource.org/licenses/mit-license.html MIT License
 */
class UserTimeZone
{

    /**
     * Time format to be used
     *
     * @var string
     */
    protected $format = null;

    /**
     * Timezone to be used
     *
     * @var string
     */
    protected $defaultTimeZone = null;

    /**
     * @param string $format
     * @param string $defaultTimeZone
     */
    public function __construct($format = 'Y-m-d h:i:s P', $defaultTimeZone = 'UTC')
    {
        $this->format = $format;
        $this->defaultTimeZone = $defaultTimeZone;
    }

    /**
     * Returns all timezones in an array.
     * Can be used to construct a dropdown of all timezones
     * for users to select
     *
     * @return array
     */
    public function getTimeZones()
    {
        return DateTimeZone::listIdentifiers();
    }

    /**
     * Sets dates in GMT format.
     * Should be used when saving dates in database for example.
     *
     * @param $date
     * @return string
     */
    public function setDate($date)
    {
        $date = new DateTime($date, new DateTimeZone($this->defaultTimeZone));
        $date->setTimezone(new DateTimeZone('GMT'));

        return $date->format($this->format);
    }

    /**
     * Gets data based on users' timezones.
     * Should be used when showing dates in pages for example.
     *
     * @param $date
     * @return string
     */
    public function getDate($date)
    {
        $date = new DateTime($date, new DateTimeZone('GMT'));
        $date->setTimezone(new DateTimeZone($this->defaultTimeZone));

        return $date->format($this->format);
    }
}

It is self-explanatory. Read the comments in class please.

Usage:

$format = 'd-m-Y h:i:s';
$userTZ = 'Asia/Karachi'; // user's time zone
$tz = new UserTimeZone($format, $userTZ);

$date = $tz->setDate('2015-01-01 6:32 PM');
// save $date in db for example

$date = $tz->getDate('2015-01-01 9:32 PM');
// show $date on webapge for example







Comments powered by Disqus