Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/254.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在CakePHP中将时间字符串从本地时间转换为GMT_Php_Cakephp_Datetime - Fatal编程技术网

在CakePHP中将时间字符串从本地时间转换为GMT

在CakePHP中将时间字符串从本地时间转换为GMT,php,cakephp,datetime,Php,Cakephp,Datetime,如何在CakePHP中将日期时间字符串从用户的时区转换为GMT 我知道CakeTime和TimeHelper:它们似乎能够很好地处理从服务器时间到用户本地时间的时间字符串转换,但它们似乎没有涵盖用户提交的日期。在我的一个模型中,我有一个用户提交的时间字段,它接受日期时间格式的输入,即用户的本地时间:2014-04-07 04:48:05。我需要在提交表单后将其转换为UTC 这是我视图的控制器方法。我试图使用CakeTime::format将sized字段转换为UTC,但它将时间按正确的小时数向错

如何在CakePHP中将日期时间字符串从用户的时区转换为GMT

我知道
CakeTime
TimeHelper
:它们似乎能够很好地处理从服务器时间到用户本地时间的时间字符串转换,但它们似乎没有涵盖用户提交的日期。在我的一个模型中,我有一个用户提交的时间字段,它接受日期时间格式的输入,即用户的本地时间:
2014-04-07 04:48:05
。我需要在提交表单后将其转换为UTC

这是我视图的控制器方法。我试图使用CakeTime::format将
sized
字段转换为UTC,但它将时间按正确的小时数向错误的方向移动:不是将15:00EST转换为19:00GMT,而是在保存时将其转换为11:00(!?)

如何使用适当的PHP时区将其从本地时间转换为GMT

public function add() {
    App::uses('CakeTime', 'Utility');
    if ($this->request->is('post')) {
        $this->Post->create();
    // Change the submitted time to UTC before the post saves
        $this->request->data('Post.sighted', CakeTime::format($this->request->data['Post']['sighted'], '%Y-%m-%d %H:%M:%S', 'UTC'));
        if ($this->Post->save($this->request->data)) {
            $this->redirect(array('action' => 'view', $this->Post->id));
        } else {
            $this->Session->setFlash(__('The post could not be saved. Please, try again.'), 'flash/error');
        }
    }
    // Get local time and set the field before the view loads
    $this->request->data['Post']['sighted'] = CakeTime::format(date('Y-m-d H:i:s'), '%Y-%m-%d %H:%M:%S', 'N/A', 'America/New_York');
}

如果您了解MVC和OOP的基础知识,那么在模型层中不能使用帮助器是合乎逻辑的。致命错误只是告诉您,您试图访问一个不存在的对象,因为没有实例化这样的对象

检查API也有助于:

官方文件中也提到:

doc块中的第四个示例显示了如何使用时区

CakeTime::format('2012-02-15 23:01:01', '%c', 'N/A', 'America/New_York');
-


是的,但是CakeTime假设它提供的原始时间字符串是GMT。CakeTime就是这样:文档中没有明确提到它,但您可以指定开始日期的时区:
$this->request->data('Post.sized',CakeTime::format($this->request->data['Post']['sized'])。CakeSession read(“Auth.User.time\u zone”),“%Y-%m-%d%H:%m:%S”,“不适用”,“UTC”)
/**
 * Returns a formatted date string, given either a UNIX timestamp or a valid strtotime() date string.
 * This function also accepts a time string and a format string as first and second parameters.
 * In that case this function behaves as a wrapper for TimeHelper::i18nFormat()
 *
 * ## Examples
 *
 * Create localized & formatted time:
 *
 * {{{
 *   CakeTime::format('2012-02-15', '%m-%d-%Y'); // returns 02-15-2012
 *   CakeTime::format('2012-02-15 23:01:01', '%c'); // returns preferred date and time based on configured locale
 *   CakeTime::format('0000-00-00', '%d-%m-%Y', 'N/A'); // return N/A becuase an invalid date was passed
 *   CakeTime::format('2012-02-15 23:01:01', '%c', 'N/A', 'America/New_York'); // converts passed date to timezone
 * }}}
 *
 * @param integer|string|DateTime $date UNIX timestamp, strtotime() valid string or DateTime object (or a date format string)
 * @param integer|string|DateTime $format date format string (or UNIX timestamp, strtotime() valid string or DateTime object)
 * @param boolean|string $default if an invalid date is passed it will output supplied default value. Pass false if you want raw conversion value
 * @param string|DateTimeZone $timezone Timezone string or DateTimeZone object
 * @return string Formatted date string
 * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#TimeHelper::format
 * @see CakeTime::i18nFormat()
 */