0

There is a function date_default_timezone_set() in php which accepts timezone string like "UK/London". But unfortunately I have the GMT offset of the user only.

Let's say I am on GMT +5 and I want to get current time based on GMT +5. I did a lot of search on google but did not help.

Since, I do not have the time zone string I am looking for something like

$current_date = date("Y M D H:S, "GMT +5");

Please help

Shahid Karimi
  • 4,096
  • 17
  • 62
  • 104

2 Answers2

3

You can only exclusively use GMT offsets for right now, i.e. it's completely useless if the user told you his GMT offset half a year ago and you want to know now what time it is at his location. It could basically be anything within a range of a few hours. Even if you got the offset a few seconds ago, it may already be different by the time you do the calculations if you happen to hit a DST change exactly.

Having said that, you'll want to add/subtract the offset hours from the formatted time, which is really the only thing you can do.

$offset = 5;
$hours  = (gmdate('H') + $offset) % 24;

echo $hours . gmdate(':i:s Y m d');

That's just an example, you'll need to handle minute offsets as well.

NorthBridge
  • 639
  • 8
  • 20
deceze
  • 510,633
  • 85
  • 743
  • 889
2
date_default_timezone_set('GMT');
$curTime = strtotime("+5 hours");
$current_date = date("Y M D H:S, $curTime);
  1. set GMT timezone first or get GMT offset。
  2. get timestamp
  3. timestamp to date
davidnkcao
  • 31
  • 2