Codeigniter API: How to get data from mysql database based on users timezone in a Codeigniter API?
22:46 23 Feb 2021
  • I want display GMT 0 todays data users timezone wise.

  • I want data from mysql according multiple timezone. I have a GMT 0 data in a database. In my application there are multiple users globally. So i want calls data according to user timezone wise.

  • I get data by GMT 0 timezone from database and then i convert that data into perticular users timezone. I want Arizona time zone mostly so it is GMT-7. When i get todays data user wise issue is that when GMT 0 and GMT-7 date is same result shows diffrent and if date is not same then result is correct

  • I attached my Codeigniter API code of get chart data of todays calls. So if you have any solution Your thoughts are very welcome.

     
     public function __construct()
     {
         date_default_timezone_set('UTC');
     }
    
     
     public function getChartNumberOfCalls($company_uuid,$timezoneid){
         if($_SERVER['REQUEST_METHOD']=='OPTIONS'){
             return json_encode('OK');
         }
         $request_body = file_get_contents('php://input');
    
         $filterhrs = $this->api_model->convert_to_GMT($startdate,1,$timezoneid);
    
         $currdate = $this->api_model->display_to_GMT(date('Y-m-d H:i:s'),1,$timezone1);
         $NumberOfCalls = $this->api_model->getChartNumberOfCalls($company_uuid,$currdate);
    
         $calls = array();
           $hrsarr = array();
           $keyhrsarr = array();
           foreach ($NumberOfCalls as $key => $value) {
             if($value['month']<10)
             $value['month'] ='0'.$value['month'];
             if($value['Hours']<10)
             $value['Hours'] ='0'.$value['Hours'];
             $convertdate = $value['year'].'-'.$value['month'].'-'.$value['day'].' '.$value['Hours'].':00:00';
             $date =  $this->api_model->display_to_GMT($convertdate,1,$timezone1);
             $calls[$key]['date'] = $date;
             $keyhrsarr[date('H',strtotime($date))]['keydate'] = date('H',strtotime($date));
             $keyhrsarr[date('H',strtotime($date))]['usage'] = $value['usage'];
             $calls[$key]['usage'] = $value['usage'];
             $calls[$key]['hrs'] = date('H',strtotime($date));
             $hrsarr[$key] = date('H',strtotime($date));
           }
    
           $arr = array();
           $iTimestamp = strtotime($filterhrs);
           $j = 0;
           $newdatepre='';
           for ($i = 0; $i <= 23; $i++) {
               $hrs = date('Y-m-d H:i:s', $iTimestamp);  
               $hrs = $this->api_model->display_to_GMT($hrs,1,$timezone1);
               $hrs = date('H', strtotime($hrs));  
               if(in_array($hrs,$hrsarr)){
                 $arr[] = (int)@$keyhrsarr[$hrs]['usage'];               
              }else{
                 $arr[] = 0;   
               }
               $j++;
               $iTimestamp += 3600;
           }
          $arr1['data'] = $arr;
          $this->output
                 ->set_content_type('application/json')
                 ->set_output(json_encode($arr1['data']));
     }
    
     
     public function getChartNumberOfCalls($company_uuid,$currdate){
         if(date('Y-m-d')==date('Y-m-d',strtotime($currdate))){
             $start_date = date('Y-m-d 00:00:00');
             $end_date = date('Y-m-d H:i:s');           
         }else{
             $start_date = date('Y-m-d H:i:s', strtotime('-1 day'));
             $end_date = date('Y-m-d H:i:s');
         }
    
         $sql =$this->db->query("SELECT HOUR(start_stamp) AS Hours,day(start_stamp) AS day,month(start_stamp) AS month,year(start_stamp) AS year,COUNT(*) AS `usage` FROM calls WHERE (start_stamp BETWEEN '".$start_date."' AND '".$end_date."') and company_uuid='".$company_uuid."' GROUP BY HOUR(start_stamp), day( start_stamp ) ORDER BY start_stamp desc");
    
         return $sql->result_array();
       }
    
    
    
       
       function display_to_GMT($convertdate,$offset,$timezone_id){
             $this->db->select('gmtoffset');
             $this->db->from('timezone');
             $this->db->where('id',$timezone_id);
             $query = $this->db->get();
             $timezone_offset = $query->result();
    
             $USER_GMT = $timezone_offset['0']->gmtoffset;
             // echo $USER_GMT;exit;
             // $USER_GMT = '19800'; // 5:30 Indian Timezone
             $SERVER_GMT='0';
             $date_time_array = getdate(strtotime($convertdate));
             $hours = $date_time_array['hours'];
             $minutes = $date_time_array['minutes'];
             $seconds = $date_time_array['seconds'];
             $month = $date_time_array['mon'];
             $day = $date_time_array['mday'];
             $year = $date_time_array['year'];
             $timestamp = mktime($hours, $minutes, $seconds, $month, $day, $year);
             $timestamp = $timestamp+($USER_GMT-$SERVER_GMT);
             // echo $convertdate.'===='.$timestamp;exit;
             // $date = date("Y-m-d H:i:s", $timestamp);
             if ($offset == 1) {
               $date = date( "Y-m-d H:i:s", $timestamp );
             } else {
               $date = date( "Y-m-d", $timestamp );
             }        
             return $date;
           }
    
          
           function convert_to_GMT($currDate, $fulldate = 1, $timezone_id = '') {
             $SERVER_GMT = '0';
             $this->db->select('gmtoffset');
             $this->db->from('timezone');
             $this->db->where('id',$timezone_id);
             $query = $this->db->get();
             $timezone_offset = $query->result();
    
             $USER_GMT = $timezone_offset ['0']->gmtoffset;
    
             $date_time_array = getdate ( strtotime ( $currDate ) );
             $hours = $date_time_array ['hours'];
             $minutes = $date_time_array ['minutes'];
             $seconds = $date_time_array ['seconds'];
             $month = $date_time_array ['mon'];
             $day = $date_time_array ['mday'];
             $year = $date_time_array ['year'];
             $timestamp1 = mktime ( $hours, $minutes, $seconds, $month, $day, $year );
             // echo '===='.$timestamp1.'===ok';exit;
             $timestamp = $timestamp1 - ($SERVER_GMT + $USER_GMT);
    
             if ($fulldate == 1) {
               $date = date ( "Y-m-d H:i:s", $timestamp );
             } else {
               $date = date ( "Y-m-d", $timestamp );
             }
             return $date;
           }
    
php angular codeigniter codeigniter-3