当前位置: 首页>>代码示例>>PHP>>正文


PHP Time::create方法代码示例

本文整理汇总了PHP中Cake\I18n\Time::create方法的典型用法代码示例。如果您正苦于以下问题:PHP Time::create方法的具体用法?PHP Time::create怎么用?PHP Time::create使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Cake\I18n\Time的用法示例。


在下文中一共展示了Time::create方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。

示例1: update_datetime_fields_timezone

 /**
  * When default server timezone is set as UTC (-> database stores dates as UTC) and the user's locale is not UTC, dates properties are updated in forms in the user's locale
  * As CakePHP 3.0.0-alpha2 marshalls dates values sent from forms in the default locale UTC, the timezones must be corrected to be saved correctly
  * 
  * Using this Listener allows to change the saved datetime timezones easily
  * 
  * Usage:
  *      AppController:
  *          
  *          Configure::write('display_timezone', 'Europe/Zurich);
  *          
  *      UsersController:
  *          
  *          $this->Users->eventManager()->attach(new TimezoneEventListener());
  *          
  *          $user = $this->Users->patchEntity($user, $this->request->data);
  * 
  * @param Event $event
  * @param EntityInterface $entity
  * @param unknown $options
  * @param Validator $validator
  * @return boolean
  */
 public function update_datetime_fields_timezone(Event $event, EntityInterface $entity, $options = [], Validator $validator)
 {
     $display_timezone = isset($this->_config['display_timezone']) ? $this->_config['display_timezone'] : Configure::read('display_timezone');
     $default_timezone = date_default_timezone_get();
     if (!empty($display_timezone) && $display_timezone != $default_timezone) {
         $data = $entity->toArray();
         foreach ($data as $property => $value) {
             if (!in_array($property, $this->_config['skipped_properties'])) {
                 $type = $event->subject()->schema()->columnType($property);
                 if ($type == 'datetime') {
                     if (is_a($data[$property], 'Cake\\I18n\\Time')) {
                         /*
                          * At this step, as the datetime has already been marshalled, the datetime has the value selected in the view, but its timezone is wrong
                          *
                          * Create a new Time object with the values from the saved datetime, but with the timezone used for display
                          */
                         $timezoned_value = Time::create($data[$property]->year, $data[$property]->month, $data[$property]->day, $data[$property]->hour, $data[$property]->minute, $data[$property]->second, $display_timezone);
                     } elseif (is_array($data[$property])) {
                         /*
                          * Actually if the Listener is attached to 'Model.beforeValidate', we probably never fall here as the date array has already been marshalled
                          */
                         $data[$property]['year'] = isset($data[$property]['year']) ? $data[$property]['year'] : null;
                         $data[$property]['month'] = isset($data[$property]['month']) ? $data[$property]['month'] : null;
                         $data[$property]['day'] = isset($data[$property]['day']) ? $data[$property]['day'] : null;
                         $data[$property]['hour'] = isset($data[$property]['hour']) ? $data[$property]['hour'] : null;
                         $data[$property]['minute'] = isset($data[$property]['minute']) ? $data[$property]['minute'] : null;
                         $data[$property]['second'] = isset($data[$property]['second']) ? $data[$property]['second'] : null;
                         $timezoned_value = Time::create($data[$property]['year'], $data[$property]['month'], $data[$property]['day'], $data[$property]['hour'], $data[$property]['minute'], $data[$property]['second'], $display_timezone);
                     }
                     if (isset($timezoned_value)) {
                         /*
                          * Transform the Time object to UTC timezone
                          */
                         $timezoned_value->setTimezone($default_timezone);
                         $entity->set($property, $timezoned_value);
                     }
                 }
             }
         }
     }
     return true;
 }
开发者ID:alaxos,项目名称:cakephp3-libs,代码行数:65,代码来源:TimezoneEventListener.php


注:本文中的Cake\I18n\Time::create方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。