assistance with a timestamp php function

2019-08-22 08:45发布

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条回答
▲ chillily
2楼-- · 2019-08-22 09:30

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.

查看更多
登录 后发表回答