【问题标题】:time() not returning daylight savings valuetime() 不返回夏令时值
【发布时间】:2016-08-02 11:59:18
【问题描述】:

在设置以下内容后,我希望 time() 返回英国夏令时的纪元时间,但我得到的是 UTC/GMT。

date_default_timezone_set("Europe/London");

我将值存储在针对 BST 调整的数据库中,需要比较 time() 返回的值。该系统目前无法按预期工作,因为存在小时差异。 time() 函数需要返回 BST 时间,或者我需要一种方法来检测伦敦是否处于 BST 中,以便将 3600 添加到 time() 的结果中。任何一种解决方案都会受到欢迎。

date_default_timezone_set("Europe/London");

// Wordpress - Getting a value from the DB
$min_deadline = get_post_meta($id,'wdm_deadline_min',true);

$min_signup_check = false;
if (!empty($min_deadline) && time() >= strtotime($min_deadline))
    $min_signup_check = true;

【问题讨论】:

  • time() 就是这样工作的。改用 DateTime 对象
  • 夏时制带来的烦人问题似乎没有尽头。
  • @JeffPuckettII 夏令时是一个早于人造光的过时概念。它只是在这里引起问题。
  • 真的!至少您正在使用以 10 为基础的公制,而我们美国人仍然坚持使用不稳定的英制。浪费了太多时间编程单位/时间转换。你应该搜索一个库而不是自己滚动。
  • @apokryfos new DateTime('now', new DateTimeZone("Europe/London")) // 给我 UTC/GMT。

标签: php wordpress timezone


【解决方案1】:

在下面找到这两种方法,现在使用 getoffset 返回 GMT 和 BST 之间的差异。

function getOffset($tzId, $time = null) {
    if($time == null){
        $time = gmdate('U');
    }

    $tz = new DateTimeZone($tzId);

    $transition = $tz->getTransitions($time);

    return $transition[0]['offset'];
}

function getDST($tzId, $time = null) {
    if($time == null){
        $time = gmdate('U');
    }

    $tz = new DateTimeZone($tzId);

    $transition = $tz->getTransitions($time);

    return $transition[0]['isdst'];
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-10-16
    • 2021-01-20
    • 2015-04-14
    • 2011-04-29
    • 2014-05-19
    • 1970-01-01
    • 2013-06-14
    • 2016-11-25
    相关资源
    最近更新 更多