Forem

Sarfraz Ahmed
Sarfraz Ahmed

Posted on • Originally published at codeinphp.github.io on

1 1

Managing Times for Users in PHP

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);
    }
}
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Image of Timescale

🚀 pgai Vectorizer: SQLAlchemy and LiteLLM Make Vector Search Simple

We built pgai Vectorizer to simplify embedding management for AI applications—without needing a separate database or complex infrastructure. Since launch, developers have created over 3,000 vectorizers on Timescale Cloud, with many more self-hosted.

Read full post →

Top comments (0)

Image of Docusign

🛠️ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay