#1
  1. A Change of Season
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    3,479
    Rep Power
    221

    Setting timezone With Codeigniter


    Hi;

    I read in a couple places to set the timezone in CI, use config file like
    PHP Code:
    date_default_timezone_set('America/New_York'); 

    However, I get users to set the timezone in their account like this


    PHP Code:
    $this->data['timezones'] = DateTimeZone::listIdentifiers(DateTimeZone::ALL); 
    Code:
    <form class="form-inline" action = "<?php echo base_url('account/update');?>" method = "post">
      <select name = "timezones" class="form-control">
        <?php
          foreach($timezones as $tz=>$val)
            {
             ?><option value="<?php echo $val;?>" 
              <?php 
              if($details['timezone']==$val)
                {
                  ?> selected = "selected" <?php
                }
                ?>
                ><?php echo $val;?></option><?php
            }?>
      </select> &nbsp;&nbsp;<input type="submit" class="btn btn-danger btn-sm" value="Update Timezone >>">
     </form>

    I am pretty sure it's not right to access the database before loading the config file.

    So how can I load the timeszone from database before loading config?

    Tanx
  2. #2
  3. Wiser? Not exactly.
    Devshed God 2nd Plane (6000 - 6499 posts)

    Join Date
    May 2001
    Location
    Bonita Springs, FL
    Posts
    6,252
    Rep Power
    4150
    If you want user specific time zones, then your approaching it problem the wrong way. You should set your default timezone to something standard that doesn't change. UTC is ideal, but it could be whatever you want.

    Then, when you want to display a time to the user, use the DateTime class's setTimezone method to switch to their selected timezone and then output the date and time using the format method.
    Recycle your old CD's



    If I helped you out, show some love with some reputation, or tip with Bitcoins to 1N645HfYf63UbcvxajLKiSKpYHAq2Zxud
  4. #3
  5. A Change of Season
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    3,479
    Rep Power
    221
    Originally Posted by kicken
    If you want user specific time zones, then your approaching it problem the wrong way. You should set your default timezone to something standard that doesn't change. UTC is ideal, but it could be whatever you want.

    Then, when you want to display a time to the user, use the DateTime class's setTimezone method to switch to their selected timezone and then output the date and time using the format method.
    Ok, I am missing one part. See if you can please explain.

    This system is all time and date based.

    Like clients can open and close pages based on specific time etc (as you can tell from my other posts).

    So here I set the default timezone in the config to date_default_timezone_set('America/New_York');

    They have the option to change the timezone from their account.

    This means all their visitors experience the website in that timezone.

    I see you show the steps but how do I do it globally so everything in the site changes without me having to go through the whole site?

    How can I get the client's profile timezone and over-write the default?

    Thanks
  6. #4
  7. Wiser? Not exactly.
    Devshed God 2nd Plane (6000 - 6499 posts)

    Join Date
    May 2001
    Location
    Bonita Springs, FL
    Posts
    6,252
    Rep Power
    4150
    Find some way with code igniter to run a bit of code that looks up their profile and sets the timezone. For example, in Symfony I'd probably attach a bit of code to the kernel.request event which is run at the beginning of every request.

    It doesn't really mater where you call date_default_timezone_set at so long as you do so before you start messing around with date and time stuff.
    Recycle your old CD's



    If I helped you out, show some love with some reputation, or tip with Bitcoins to 1N645HfYf63UbcvxajLKiSKpYHAq2Zxud
  8. #5
  9. A Change of Season
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    3,479
    Rep Power
    221
    Hi;

    I ended up doing this. Storing them in the database per user.

    Since every page is owned by a user, then getting timezone can be easy.

    I think this closer to what you suggested:

    On members area, when I check if they're logged or not:
    PHP Code:
    class Login_library 
        
    {
              public function 
    is_logged()
                {
                  
    $CI =& get_instance();
                  if(!isset(
    $_SESSION['user_id']))
                    {
                       
    date_default_timezone_set('America/New_York');    
                       if(
    strpos(current_url(), 'login') === false )
                         {
                            
    redirect(base_url('login'), 'refresh');
                         }
                    }
                  else 
                    {
                      
    $sql "SELECT * FROM launch_owners WHERE id = ?";
                      
    $query_timezone $CI->db->query($sql, array($_SESSION['user_id']));
                      
    date_default_timezone_set(''.$query_timezone->result_array()[0]['timezone'].'');
                    }  
                }
        } 
    And on public page, there is only 1 public page (which is only accessible via link like: http://local.exmple.com/test-mode-eg...52de430bb6d/20)
    PHP Code:
    private function set_timezone($hashed_owner_email)
            {
                
    $sql "SELECT * FROM launch_owners WHERE MD5(email) = ?";
                          
    $query_timezone $this->db->query($sql, array($hashed_owner_email));
                          
    date_default_timezone_set(''.$query_timezone->result_array()[0]['timezone'].'');
            } 
    Works fine.

    Thoughts?

IMN logo majestic logo threadwatch logo seochat tools logo