开发者

Calculate time from timezones in php

Hai

I have the system with employees having different timezones in their profile. I would like to show the date according to their timezones specified. The GMT time zone values are placed in the database. could you guys help开发者_高级运维 me


Let's say your server is located in the USA and stores dates in the database with local US GMT -7.

So you send the local offset -7 and the offset of the employee country, lets say somewhere in Europe +2.

So basically you just need to know what is -7 -2 and its 9 hours offset, so each date you have stored in the database must be added 9 hours to represent the time in Europe.

This will count the offset:

fnGetOffset($source_gmt_offset ,$fltGmtOffset)
{
    $intGmtOffest = $source_gmt_offset - $fltGmtOffset; 
    if($intGmtOffest <> 0)
    {
        return $intGmtOffest;
    }
}

After you count the offset you just need to get the date from the db and add to it the offset.

Like that:

$intUnixTimeStamp += $intGmtOffest*60*60;

So this is the proper way to do it with PHP.

And if you want to query the server based on user selected time, you need to go back -9 .


Check out PHP's timezone-setting functions, including date_timezone_set.


in addition to Dav's answer i propose to store all time data in "timestamp" mysql datatype because it automatically handles timezones


Let say:

Your Company local time is some where in USA gmt (-6) and we have an employee at Holland with gmt (+2).

So what you will want to do is:

  1. Save the event date at the company local time at your database.

  2. Now when you query your db for available dates, first check the employee country to get his offset.

    • If its the Holland employee’s you figure its +2 , you know that all dates in your database are stored as local company time gmt -8. So it’s actually a -6 -+2 offset from gmt = -8 Now if we want to go from server time to the employee in Holland we just add the 8 hours offset to the time.

Best thing is to apply it on the sql query that should be like:

$symb= "+";
$intGmtInterval= 8;

 DATE_FORMAT(CONVERT_TZ(my_event,'+0:00','$symb$intGmtInterval:00'),'%W')  

So this actually will add to any date resulted in your main query the 8 hour offset .

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜