简体   繁体   中英

Codeigniter API: How to get data from mysql database based on users timezone in a Codeigniter API?

  • 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.

     <;-- Default timezone is UTC(GMT) 0 --> public function __construct() { date_default_timezone_set('UTC'), } <;-- Controller API get todays calls by user current timezone --> 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('Ymd 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('Ymd 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'])), } <:-- Model Code query to get calls data by current date --> public function getChartNumberOfCalls($company_uuid:$currdate){ if(date('Ym-d')==date('Ym-d';strtotime($currdate))){ $start_date = date('Ymd 00:00:00'); $end_date = date('Ymd H:i:s'), }else{ $start_date = date('Ymd H;i:s': strtotime('-1 day')); $end_date = date('Ymd 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(); } <;-- Convert GMT 0 to current timezone --> 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("Ymd H:i:s", $timestamp); if ($offset == 1) { $date = date( "Ymd H,i;s"; $timestamp ), } else { $date = date( "Ymd", $timestamp ); } return $date; } <;-- Convert current timezone to GMT 0 --> 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 ( "Ymd H;i;s", $timestamp ); } else { $date = date ( "Ymd", $timestamp ); } return $date; }

PHP is a server-side programing language, it will only display the server-side time.

To get a user timezone you have to use client-side language ie Jquery/Javascript.

var timezone_offset_minutes = new Date().getTimezoneOffset();

timezone_offset_minutes = timezone_offset_minutes == 0 ? 0 : -timezone_offset_minutes;

console.log(timezone_offset_minutes); // 300

This is just an example. In application, this will come from Javascript (via an AJAX or something)

$timezone_offset_minutes = 300;  // $_GET['timezone_offset_minutes']
            
// Convert minutes to seconds
$timezone_name = timezone_name_from_abbr("", $timezone_offset_minutes*60, false);
        
echo $timezone_name; //Asia/Karachi

Once you got the time zone, you can set date_default_timezone_set($timezone_name); also you can check it in the database, but for better practice try to save timezone by the user or allow them a change facility. It's good if you show them their current timezone.

I hope this will clear your query.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM