當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。