简体   繁体   English

协助时间戳php功能

[英]assistance with a timestamp php function

I need to convert a timestamp to UTC-5 我需要将时间戳转换为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. $offset是用户的时区,值为-12到12. $ds是夏令时,如果启用,则会额外增加一小时。 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. 我创建了这个函数,但我认为它会根据用户的时区将UTC-5时间戳转换为新的时间戳...我需要将函数反转,以便它返回UTC-5的时间戳。 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;
    }
}

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? 你见过DateTimeDateTimeZone类吗? In addition to doing basic math, they will help you with the particular insanities of this realm of programming (per-county DST! Leap years!). 除了做基础数学,他们还会帮助你解决这个编程领域中的特殊问题(每个县DST!闰年!)。

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. 根据定义,UNIX时间戳独立于时区,DST等。它被精确地定义为自给定参考日期以来经过的秒数,以及时间流(尽管相对论效果;-)在方面是不变的到位置或立法者的特殊特质。

Maybe if you can describe in more detail what your actual goal is then we might be able to suggest a more coherent approach. 也许如果你能更详细地描述你的实际目标是什么,那么我们可以建议一个更连贯的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM