【问题标题】:CakePHP 2.6 DateTime input and user timezoneCakePHP 2.6 DateTime 输入和用户时区
【发布时间】:2015-05-29 08:54:31
【问题描述】:

给定一个在不同时区运行的 Web 应用程序,CakePHP 2.x dateTime 输入是否可以在编辑时显示给用户显示的值,以在他们的时区中。

例如。如果我有一条存储在数据库中的记录为 appointment_date(将在 UTC 时区中),如果我作为时区 America/Jamaica 中的用户,我想使用显示我的输入来编辑该记录我的时区中的值,例如当我这样做时:

<?php echo $this->Form->dateTime('appointment_date'); ?>

当然,假设我们将在保存之前转换回 UTC 时区。

【问题讨论】:

  • 当你说“显示在他们的时区”时,你的意思是时间下拉菜单中的默认值显示我的本地时间(而不是 UTC)?
  • 正确,但请注意这将是记录的“现有”值。想象一下编辑一个名为约会日期的字段(最初以 UTC 格式保存在数据库中)

标签: php datetime cakephp cakephp-2.6


【解决方案1】:

我在这里创建了一个要点:https://gist.github.com/antoniovassell/fed29e782be33b7eb5ab 这允许使用扩展 FormHelper 的自定义帮助程序类以抽象方式处理 dateTime 输入时区转换。

public function dateTime($fieldName, $dateFormat = 'DMY', $timeFormat = '12', $attributes = array()) {
        $attributes = parent::value($attributes, $fieldName);
        if (!empty($attributes['value'])) {
            $attributes['value'] = CakeTime::format('Y-m-d H:i:s', $attributes['value']);
        }
        return parent::dateTime($fieldName, $dateFormat, $timeFormat, $attributes);
    }

因此,除了使用 TimeHelper 手动将表单值转换为用户时区,然后将值设置为输入,我们还可以通过覆盖表单 dateTime 函数来自动执行此操作。每当我们调用echo $this-&gt;Form-&gt;dateTime('my_field'); 时,我们都会自动执行此操作。

【讨论】:

    猜你喜欢
    • 2013-08-06
    • 2023-03-15
    • 1970-01-01
    • 2015-06-01
    • 1970-01-01
    • 2014-03-12
    • 2018-11-13
    • 2014-11-13
    • 1970-01-01
    相关资源
    最近更新 更多