Answers.Body: <p>100% untested, but something like this should work:</p>
  
  <ol>
  <li><p>Have your model override BaseModel's <a href="https://craftcms.com/classreference/models/BaseModel#populateModel-detail" rel="nofollow noreferrer">populateModel</a>.</p></li>
  <li><p>Find the DateTime field you're interested in and get its value.</p></li>
  <li><p>Create a new <a href="http://php.net/manual/en/class.datetime.php" rel="nofollow noreferrer">DateTime</a> object and pass in the value from the <code>timeZone</code> attribute.</p></li>
  <li><p>Set the new DateTime object back on the original DateTime field.</p></li>
  <li><p>Call <code>parent::populateModel($values)</code>.</p></li>
  </ol>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-08-08T20:42:45.253Z
Answers.LastActivityDate: 2017-08-08T20:42:45.253Z
Answers.UserDisplayName: Brad Bell
Body: <p>I have a question regarding populating models, in that I have a <code>DateTime</code> field in my model that I'm pulling / populating from the database. The <code>DateTime</code> value does not include the timezone it was set to when it was stored, its stored in UTC.</p>
  
  <p>I have the original timezone value (Not the same as the CRAFT timezone but the users timezone that actually created the entry) stored in the model attributes as well.</p>
  
  <p>Is there a way I can extend the model so that when the data is populated I can force the correct timezone on the <code>DateTime</code> fields? </p>
  
  <p>I'm pretty sure that Craft is going to convert them to the timezone craft is set to when it pulls it into the model, but I dont want that in this case. I want it set to the TZ stored in the "timeZone" column/attribute...</p>
  
  <p>Example:</p>
  
  <p>Craft Timezone is America/Chicago</p>
  
  <p>The user's timezone is America/New_York</p>
  
  <p>The user selects a date and time and the controller creates the DateTime object with the user's selection, specifying the timezone America/New_Yrok, then tells the model to save, so it stores it in the database.</p>
  
  <p>The Database DateTime value is now stored in UTC.</p>
  
  <p>When I fetch this data and populate a model with it and reference the DateTime object, its going to be in Amercia/Chicago right? since that's what the craft system is set to, right?</p>
  
  <p>So what is the best way to set up the model so it automatically knows to use <code>$this-&gt;getAttribute('timeZone')</code> as the timezone for that field rather than the Craft system setting?</p>
  
  <p>EDIT:</p>
  
  <p>This is what I ended up doing that appears to work. I added this to my model:</p>
  
  <pre><code>public static function populateModel($values) {
      $model = PARENT::populateModel($values);
  
      /** @var $startTime DateTime */
      $startTime = $model-&gt;getAttribute('startTime');
      if($startTime) {
          $startTime-&gt;setTimezone($model-&gt;timeZone);
          $model-&gt;setAttribute('startTime', $startTime);
      }
      /** @var $endTime DateTime|null */
      $endTime = $model-&gt;getAttribute('endTime');
      if($endTime) {
          $endTime-&gt;setTimezone($model-&gt;timeZone);
          $model-&gt;setAttribute('endTime', $endTime);
      }
  
      return $model;
  }
  </code></pre>
  
Tags: <datetime><timezone>
Title: Pull DateTime field using alternate timezone?
ThreadText: # Pull DateTime field using alternate timezone?
  
  (Asked by: username_0 on 2017-08-08)
  
  I have a question regarding populating models, in that I have a `DateTime` field in my model that I'm pulling / populating from the database. The `DateTime` value does not include the timezone it was set to when it was stored, its stored in UTC.
  
  I have the original timezone value (Not the same as the CRAFT timezone but the users timezone that actually created the entry) stored in the model attributes as well.
  
  Is there a way I can extend the model so that when the data is populated I can force the correct timezone on the `DateTime` fields?
  
  I'm pretty sure that Craft is going to convert them to the timezone craft is set to when it pulls it into the model, but I dont want that in this case. I want it set to the TZ stored in the "timeZone" column/attribute...
  
  Example:
  
  Craft Timezone is America/Chicago
  
  The user's timezone is America/New\_York
  
  The user selects a date and time and the controller creates the DateTime object with the user's selection, specifying the timezone America/New\_Yrok, then tells the model to save, so it stores it in the database.
  
  The Database DateTime value is now stored in UTC.
  
  When I fetch this data and populate a model with it and reference the DateTime object, its going to be in Amercia/Chicago right? since that's what the craft system is set to, right?
  
  So what is the best way to set up the model so it automatically knows to use `$this->getAttribute('timeZone')` as the timezone for that field rather than the Craft system setting?
  
  EDIT:
  
  This is what I ended up doing that appears to work. I added this to my model:
  
  ```
  public static function populateModel($values) {
      $model = PARENT::populateModel($values);
  
      /** @var $startTime DateTime */
      $startTime = $model->getAttribute('startTime');
      if($startTime) {
          $startTime->setTimezone($model->timeZone);
          $model->setAttribute('startTime', $startTime);
      }
      /** @var $endTime DateTime|null */
      $endTime = $model->getAttribute('endTime');
      if($endTime) {
          $endTime->setTimezone($model->timeZone);
          $model->setAttribute('endTime', $endTime);
      }
  
      return $model;
  }
  
  ```
  
  **Tags:** datetime, timezone
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2017-08-08. Score: 3 | Accepted answer
  
  100% untested, but something like this should work:
  
  1. Have your model override BaseModel's [populateModel](https://craftcms.com/classreference/models/BaseModel#populateModel-detail).
  2. Find the DateTime field you're interested in and get its value.
  3. Create a new [DateTime](http://php.net/manual/en/class.datetime.php) object and pass in the value from the `timeZone` attribute.
  4. Set the new DateTime object back on the original DateTime field.
  5. Call `parent::populateModel($values)`.
  
  
  ---
  
  