How to manage timezone for different users of different countries for a php web application?

后端 未结 2 679
情深已故
情深已故 2021-01-24 23:23

I am developing web application. The application has different users from different countries.

I have used to manage thier registrations using UTC timestamp. It works fi

相关标签:
2条回答
  • 2021-01-24 23:58

    You need do few steps.

    • Database table of all countries along their timezone
    • On registration or form submit get IP address
    • Check details based on IP and get Country and Timezone
    • Save country and timezone along record information

    In this way, you can maintain the records along their creator's timezone.

    As far as expiring records based on timezone then you can check based on their timezone.

    Suppose I am in Pakistan and timezone is 5+. So expiry step should also follow my timezone.

    0 讨论(0)
  • 2021-01-25 00:04
    /**
     * @function getusertimezonedifference return timezone difference with UTC
     * @param integer $timezonedifference       optional
     * @return string time
     * @Comment Get User TimeZone Difference
     */
    public function getusertimezonedifference($timezone = "Asia/Kolkata", $changetoUTC=false, $flg_array=false)
    {
        #$timezone = "Asia/Kolkata";+5.30
        #$timezone = "America/Anchorage";-9
        #$timezone = "America/Los_Angeles";-8
        #$timezone = "America/Dawson_Creek";-8
        #$timezone = "America/Chicago";-6
        #$timezone = "America/New_York";-5
        #$timezone = "America/Halifax";-4
        #$timezone = "America/St_Johns";#3.30
        $dayLightFlag = false;
        $system_timezone = date_default_timezone_get();
        $local_timezone = "UTC";
        date_default_timezone_set($local_timezone);
        $local = date("Y-m-d H:i:s");
        date_default_timezone_set("GMT");
        $gmt = date("Y-m-d H:i:s");
        date_default_timezone_set($timezone);
        $required = date("Y-m-d H:i:s");
        date_default_timezone_set($system_timezone);
        $diff1 = (strtotime($gmt) - strtotime($local));
        $diff2 = (strtotime($required) - strtotime($gmt));
        if($changetoUTC) {$diff2 =  (-1*$diff2);}
        // extract hours
        $hours = floor($diff2 / (60 * 60));
        // extract minutes
        $divisor_for_minutes = $diff2 % (60 * 60);
        $minutes = floor($divisor_for_minutes / 60);
        // extract the remaining seconds
        $divisor_for_seconds = $divisor_for_minutes % 60;
        $seconds = ceil($divisor_for_seconds);
    
        //create string HH:MM:SS
        $ret = $hours.":".(($minutes>9)?$minutes:"0$minutes").":".(($seconds>9)?$seconds:"0$seconds");
        if($flg_array){$ret = array($hours, ((abs($minutes)>9)?$minutes:"0$minutes"), ($seconds>9)?$seconds:"0$seconds");}
        return ($ret);
    }
    
    0 讨论(0)
提交回复
热议问题