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

one text

Solution:

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 i.e 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.

Source