使用时间戳 php 函数的帮助 [英] assistance with a timestamp php function

查看:28
本文介绍了使用时间戳 php 函数的帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将时间戳转换为 UTC-5

I need to convert a timestamp to UTC-5

$offset 是用户的时区,值从 -12 到 12.$ds 是夏令时,如果打开,它将增加一个小时.我制作了这个函数,但我认为它根据用户的时区将 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. 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;
    }
}

推荐答案

哇...谈论重新发明轮子!而且在一个众所周知难以正确处理的领域(日期/时间操作)同样如此.

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

您是否看过 DateTimeDateTimeZone 类?除了做基础数学之外,他们还将帮助您解决这个编程领域的特殊问题(每个县的夏令时!闰年!).

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!).

不过,我不得不问,你为什么要这样做?根据定义,UNIX 时间戳与时区、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.

也许如果您能更详细地描述您的实际目标,那么我们可能会建议一个更连贯的方法.

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

这篇关于使用时间戳 php 函数的帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆