Part of the EllisLab Network
x
 
Create New Page
 View Previous Changes    ( Last updated by CI TaMeR )

Category:Libraries -> TimeTamer

Category:Libraries
Category:Libraries -> TimeTamer

Updating my contributions on the wiki has become a nightmare.
Please check my svn at google for the latest revisions:
http://code.google.com/p/tamer/
CI_TaMeR SVN


Would you like to show your users date and time in their local time zone?
This took me a long time to figure out.
Problem is you guessed it Microsoft. NT, XP 95 what ever, they are all different, anyway so here it goes:

1) We get the Date via JavaScript
2) we place a cookie via JavaScript.
3) We read the cokkie via PHP
4) we extract the timezone
5) We set a cookie and a session var with time timezone
6) We place a putenv in to our view page.


When you get date and time from mysql you don’t have to do any thing if you are in a view file, but from the controller you can call this to convert mysql to local time:

# To convert mysql timedate to users localtime:
$this->timetamer->ldate($row->Time);
# To get local time now:
$this->timetamer->ldate();

I believe that mysql on a internet server should be set to GMT timezone.
To do so edit the file my.cnf
On debian located at /etc/mysql/my.cnf
Right below mysqld_safe add the line ‘timezone = Europe/London’ like this:

[mysqld_safe]
timezone
= Europe/London

Here is the code:

First if your system is not on GMT time you will have place this in to your config file:

$config['time_reference'] = 'gmt';


And/Or add this in to the autoload.php

/******************************************\
*   Added for TimeTamer setting ENV to GMT  
/******************************************/
putenv('TZ=GMT');
/******************************************/
$autoload['libraries'] = array('TimeTamer');

Place this on top of your view/header file

<?php
    $this
->timetamer->setTZ();
?>


This goes anywhere in your views head area:
The echo line gets removed by the wiki so here it is just entcoded:
  echo ”< script type=\“text/javascript\” src=\”/public/js/$v.js\”></script>”;

<?php
if(isset($tpljs)){
    
foreach($tpljs as $v){
        
echo "</script>";
   
}
}
?>
</head>

Call this one /public/js/timetamer.js

function createCookie(name, value, days)
{
  
if (days) {
    
var date = new Date();
    
date.setTime(date.getTime()+(days*24*60*60*1000));
    var
expires = "; expires="+date.toGMTString();
    
}
  
else var expires = "";
   =
name+"="+value+expires+"; path=/";
}

var date = new Date()
var
gmt = date.getTimezoneOffset()/60
createCookie
("TimeTamerGMT", gmt, "365");
createCookie("TimeTamerTZ", date, "365");

And finally this goes in to your Libraries folder

<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class
Timetamer
{

/*******************************************************************************
    TimeTaMeR Class ver. 0.1
    Copyright (c) 2007, Dennis T. Kaplan
    All rights reserved.
********************************************************************************
    Redistribution and use in source and binary forms,
    with or without modification, are permitted provided that the following conditions are met:

        * Redistributions of source code must retain the above copyright notice,
        this list of conditions and the following disclaimer.
        
        * Redistributions in binary form must reproduce the above copyright notice,
        this list of conditions and the following disclaimer in the documentation and/or
        other materials provided with the distribution.
        
        * Neither the name of the finalwebsites.com nor the names of its contributors may
        be used to endorse or promote products derived from this software without specific
        prior written permission.

    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

    _________________________________________________________________________
****************************************************************************
* To get users local time from mysql:
* $this->timetamer->ldate($row->Time);
* To get local time now:
* $this->timetamer->ldate();
****************************************************************************/

    
var $time;
    var
$abbr;

    function
setVars(){
        $this
->time = time(); // This is GMT time see config file
        
$this->abbr = array(
            
'ACST', 'ACT', 'ADDT', 'ADMT', 'ADT', 'AFT', 'AHDT',
            
'AHST', 'AKDT', 'AKST', 'AKTST', 'AKTT', 'ALMST',
            
'ALMT', 'AMST', 'AMT', 'ANAST', 'ANAT', 'ANT', 'AOT',
            
'APT', 'AQTST', 'AQTT', 'ARST', 'ART', 'ASHST', 'ASHT',
            
'AST', 'AWT', 'AZOMT', 'AZOST', 'AZOT', 'AZST', 'AZT',
            
'BAKST', 'BAKT', 'BDST', 'BDT', 'BEAT', 'BEAUT', 'BMT',
            
'BNT', 'BORT', 'BORTST', 'BOST', 'BOT', 'BRST', 'BRT',
            
'BST', 'BTT', 'BURT', 'CANT', 'CAPT', 'CAST', 'CAT',
            
'CAWT', 'CCT', 'CDDT', 'CDT', 'CEMT', 'CEST', 'CET',
            
'CGST', 'CGT', 'CHADT', 'CHAST', 'CHAT', 'CHDT', 'CHOST',
            
'CHOT', 'ChST', 'CIT', 'CJT', 'CKHST', 'CKT', 'CLST',
            
'CLT', 'CMT', 'COST', 'COT', 'CPT', 'CST', 'CUT',
            
'CVST', 'CVT', 'CWT', 'CXT', 'DACT', 'DAVT', 'DDUT',
            
'DMT', 'DUSST', 'DUST', 'EASST', 'EAST', 'EAT', 'ECT',
            
'EDDT', 'EDT', 'EEST', 'EET', 'EGST', 'EGT', 'EHDT',
            
'EIT', 'EPT', 'EST', 'EWT', 'FFMT', 'FJST', 'FJT',
            
'FKST', 'FKT', 'FMT', 'FNST', 'FNT', 'FORT', 'FRUST',
            
'FRUT', 'GALT', 'GAMT', 'GBGT', 'GEST', 'GET', 'GFT',
            
'GHST', 'GILT', 'GMT', 'GMT+1', 'GMT+10', 'GMT+11', 'GMT+12',
            
'GMT+2', 'GMT+3', 'GMT+4', 'GMT+5', 'GMT+6', 'GMT+7', 'GMT+8',
            
'GMT+9', 'GMT-1', 'GMT-10', 'GMT-11', 'GMT-12', 'GMT-13', 'GMT-14',
            
'GMT-2', 'GMT-3', 'GMT-4', 'GMT-5', 'GMT-6', 'GMT-7', 'GMT-8',
            
'GMT-9', 'GST', 'GYT', 'HADT', 'HAST', 'HDT', 'HKST',
            
'HKT', 'HMT', 'HOVST', 'HOVT', 'HPT', 'HST', 'HWT',
            
'ICT', 'IDDT', 'IDT', 'IHST', 'IMT', 'IOT', 'IRDT',
            
'IRKST', 'IRKT', 'IRST', 'ISST', 'IST', 'JAVT', 'JDT',
            
'JMT', 'JST', 'KART', 'KAST', 'KDT', 'KGST', 'KGT',
            
'KIZST', 'KIZT', 'KMT', 'KOST', 'KRAST', 'KRAT', 'KST',
            
'KUYST', 'KUYT', 'KWAT', 'LHST', 'LINT', 'LKT', 'LMT',
            
'Local ti', 'LONT', 'LRT', 'LST', 'MADMT', 'MADST', 'MADT',
            
'MAGST', 'MAGT', 'MALST', 'MALT', 'MART', 'MAWT', 'MDDT',
            
'MDST', 'MDT', 'MEST', 'MET', 'MHT', 'MMT', 'MOST',
            
'MOT', 'MPT', 'MSD', 'MSK', 'MST', 'MUT', 'MVT',
            
'MWT', 'MYT', 'NCST', 'NCT', 'NDDT', 'NDT', 'NEGT',
            
'NEST', 'NET', 'NFT', 'NMT', 'NOVST', 'NOVT', 'NPT',
            
'NRT', 'NST', 'NUT', 'NWT', 'NZDT', 'NZMT', 'NZST',
            
'OMSST', 'OMST', 'ORAST', 'ORAT', 'PDDT', 'PDT', 'PEST',
            
'PET', 'PETST', 'PETT', 'PGT', 'PHOT', 'PHST', 'PHT',
            
'PKST', 'PKT', 'PMDT', 'PMST', 'PMT', 'PNT', 'PONT',
            
'PPMT', 'PPT', 'PST', 'PWT', 'PYST', 'PYT', 'QMT',
            
'QYZST', 'QYZT', 'RET', 'RMT', 'ROTT', 'SAKST', 'SAKT',
            
'SAMST', 'SAMT', 'SAST', 'SBT', 'SCT', 'SDMT', 'SGT',
            
'SHEST', 'SHET', 'SJMT', 'SLST', 'SMT', 'SRT', 'SST',
            
'STAT', 'SVEST', 'SVET', 'SWAT', 'SYOT', 'TAHT', 'TASST',
            
'TAST', 'TBIST', 'TBIT', 'TBMT', 'TFT', 'TJT', 'TKT',
            
'TLT', 'TMT', 'TOST', 'TOT', 'TRST', 'TRT', 'TRUT',
            
'TSAT', 'TVT', 'UCT', 'ULAST', 'ULAT', 'URAST', 'URAT',
            
'URUT', 'UTC', 'UYHST', 'UYST', 'UYT', 'UZST', 'UZT',
            
'VET', 'VLASST', 'VLAST', 'VLAT', 'VOLST', 'VOLT', 'VOST',
            
'VUST', 'VUT', 'WAKT', 'WARST', 'WART', 'WAST', 'WAT',
            
'WEMT', 'WEST', 'WET', 'WFT', 'WGST', 'WGT', 'WIT',
            
'WMT', 'WST', 'YAKST', 'YAKT', 'YDDT', 'YDT', 'YEKST',
            
'YEKT', 'YERST', 'YERT', 'YPT', 'YST', 'YWT', 'zzz'
            
);
    
}


    
function Timetamer(){
        $CI
=& get_instance();
        
//$CI->load->library('msg');
        
log_message('debug', "TimeTaMeR Class Initialized");
        
$tz = $CI->db_session->userdata('TZ');
        if(
$tz === FALSE)
        
{
            $tz
= $CI->input->cookie('TimeTamerTZ');
            if(
$tz)
            
{
                
if(strlen($tz) < 6)
                
{
                    $CI
->db_session->set_userdata(array('TZ' => $tz));
                
}
                
else
                
{
                    $this
->setVars();
                    
$this->getRemoteTZ($tz);                    
                
}
            }
            
else
            
{
                $CI
->tpl['tpljs'][]  = 'TimeTamer';
            
}
        }
    }
    
    
function setTZ(){
        $CI
=& get_instance();
        
$tz = $CI->db_session->userdata('TZ');
        if(
$tz != putenv('TZ='.$tz)){
            putenv
('TZ=GMT');
        
}
    }
    
    
function getRemoteTZ($cookie)
    
{
        log_message
('debug', "TimeTaMeR: Getting Remote TZ");
        
//$offset = $CI->input->cookie('TimeTamerGMT');
        
$cookie = strstr($cookie, 'GMT');
        
$cookie = strstr($cookie, '0');
        
$cookie = str_replace (' ', '', $cookie);
        
$cookie = preg_replace('/[a-z0-9()]/', '', $cookie);
        
$tz = $this->checkTZ($cookie);
        if(
$tz){
            $CI
=& get_instance();
            
$CI->db_session->set_userdata(array('TZ' => $tz));
            
setcookie('TimeTamerTZ', $tz, time()+60*60*24*365, '/');
            
setcookie('TimeTamerGMT', 'Delete', time()-60*60*24*90, '/');
        
}
    }
    
    
function checkTZ($cookie)
    
{
        log_message
('debug', "TimeTaMeR: Checking TZ");
        foreach(
$this->abbr as $v){
            $pos
= strpos($cookie, $v);
            if(
$pos !== FALSE){
                
return $v;
            
}
        }
        
return FALSE;
    
}

    
function ldate($time = FALSE)
    
{
        
// We'll remove certain characters for backward compatibility
        // since the formatting changed with MySQL 4.1
        // YYYY-MM-DD HH:MM:SS
        
if($time){
            $time
= str_replace('-', '', $time);
            
$time = str_replace(':', '', $time);
            
$time = str_replace(' ', '', $time);
            
            
// YYYYMMDDHHMMSS
            
$time = mktime(
                
substr($time, 8, 2),
                
substr($time, 10, 2),
                
substr($time, 12, 2),
                
substr($time, 4, 2),
                
substr($time, 6, 2),
                
substr($time, 0, 4)
                );
        
}else{
            $time
= time();
        
}
        $this
->setTZ();
        
$time = date('r (T)', $time );
        
putenv('TZ=GMT');
        return
$time;
    
}
}

Articles in category "Libraries -> TimeTamer":

There is 1 article for this category

Categories: