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

只愿长相守 提交于 2020-01-11 11:36:09

问题


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 fine for me.

But problem occures when I am checking the expiry date of account.

For example -

Suppose registration will be valid for 15 days and user from India whose registration date is 11 June 2014 as per Asia/Kolkata timezone, in the database table I have stored the registration date in Unix Timestamp format (Server set to UTC timezone), on 16th day I want to expire his account and when I am executing query by compairing todays date (date in timestamp, date is converted to timestamp using strtotime() function of PHP), I found there is difference in One day. How to handle these type of situation? This is not only registration problem but some other conditions also there. Also if I want to send email of registration expiry using cron job, how it will works?


回答1:


/**
 * @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);
}



回答2:


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.



来源:https://stackoverflow.com/questions/24426029/how-to-manage-timezone-for-different-users-of-different-countries-for-a-php-web

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!