assistance with a timestamp php function

自闭症网瘾萝莉.ら 提交于 2019-12-12 02:47:44

问题


I need to convert a timestamp to UTC-5

The $offset is the user's timezone the values are from -12 to 12. $ds is daylight savings, if it's on, it will add an extra hour. I made this function, but I think it converts a UTC-5 timestamp to a new timestamp based on the user's timezone...I need the function to be inverted so that it returns a timestamp in UTC-5 instead. Of course the problem is much larger than this, but here's where I'm stuck. Any way to go about it?

function input_date($timestamp)
{
    global $vbulletin;
    $timestamp = (int)$timestamp;

    if (strlen((string)$timestamp) == 10)
    {
        $hour = 3600.00;
        $offset = $vbulletin->userinfo['timezoneoffset'];//sample -8
        $ds = (int)$vbulletin->userinfo['dstonoff'];//DST values are 1 or 0
        $fluff = $hour*($offset+5.00);
        $timestamp = $timestamp+$fluff+($ds*$hour);

        return $timestamp;//return timestamp in UTC-5 format..
    }
    else
    {
        return 0;
    }
}

回答1:


Whoa... talk about reinventing the wheel! And in an area notoriously hard to get right (date/time manipulation) no less.

Have you seen the DateTime and DateTimeZone classes? In addition to doing basic math, they will help you with the particular insanities of this realm of programming (per-county DST! Leap years!).

I have to ask, though, why you are doing this? The UNIX timestamp is, by definition, independent of timezones, DST, etc. It is defined precisely as the number of seconds that have elapsed since a given reference date, and the flow of time (relativistic effects notwithstanding ;-) is invariant with respect to location or the particular idiosyncrasies of lawmakers.

Maybe if you can describe in more detail what your actual goal is then we might be able to suggest a more coherent approach.



来源:https://stackoverflow.com/questions/9526939/assistance-with-a-timestamp-php-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!