Narayan Bhandari
Narayan Bhandari

Reputation: 426

How to calculate GMT and UTC timestamp

I have been using php/ mysql for long time and i have been using unix_timestamp to store date,time values. I have created timestamp class to handle this kind of request. Source code is available on github.

By default, this class uses $_SERVER['REQUEST_TIME'] which is GMT timestamp, I believe.

My requirement are to find out Local timestamp in accordance with GMT and show to user. In other word, I need to find the local time of any user and difference with GMT in number of seconds using PHP

How do I implement these requirements in my class. Please help

Upvotes: 0

Views: 697

Answers (2)

user4628565
user4628565

Reputation:

To get an UTC timestamp try:

date_default_timezone_set("UTC");
echo date("Y-m-d H:i:s", time()); 

Upvotes: 0

Blizz
Blizz

Reputation: 8408

DateTime allows you to easily accomplish this:

// Interpret the time as UTC
$timezone = new DateTimeZone('UTC');
$datetime = new DateTime('@' . $_SERVER['REQUEST_TIME'], $timezone);

// Output as PHP's timezone
$user_timezone = new DateTimeZone(date_default_timezone_get()); // Or whatever timezone you need
$datetime->setTimeZone($user_timezone);
echo $datetime->format('Y-m-d H:i:s');

Upvotes: 1

Related Questions