php web应用程序如何为不同国家的不同用户管理时区? [英] How to manage timezone for different users of different countries for a php web application?

查看:82
本文介绍了php web应用程序如何为不同国家的不同用户管理时区?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发Web应用程序.该应用程序具有来自不同国家的不同用户.

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

我曾经使用UTC时间戳来管理他们的注册.对我来说很好.

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.

例如-

假设注册有效期为15天,并且来自印度的用户(按照亚洲/加尔各答时区的注册日期为2014年6月11日)在数据库表中以Unix时间戳格式存储了注册日期(服务器设置为UTC时区) ,在第16天,我想使他的帐户过期,并且当我通过对今天的日期进行查询(时间戳中的日期,使用PHP的strtotime()函数将日期转换为时间戳)执行查询时,我发现一天中会有区别. 如何处理这类情况?这不仅是注册问题,还存在其他一些条件. 另外,如果我想使用cron作业发送注册到期的电子邮件,它将如何工作?

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?

推荐答案

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

这篇关于php web应用程序如何为不同国家的不同用户管理时区?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆