Determine a user's timezone

前端 未结 25 3515
刺人心
刺人心 2020-11-21 07:12

Is there a standard way for a web server to be able to determine a user\'s timezone within a web page?

Perhaps from an HTTP header or part of the user-agent

25条回答
  •  青春惊慌失措
    2020-11-21 07:34

    JavaScript:

    function maketimus(timestampz)
    {
        var linktime = new Date(timestampz * 1000);
        var linkday = linktime.getDate();
        var freakingmonths = new Array();
    
        freakingmonths[0]  = "jan";
        freakingmonths[1]  = "feb";
        freakingmonths[2]  = "mar";
        freakingmonths[3]  = "apr";
        freakingmonths[4]  = "may";
        freakingmonths[5]  = "jun";
        freakingmonths[6]  = "jul";
        freakingmonths[7]  = "aug";
        freakingmonths[8]  = "sep";
        freakingmonths[9]  = "oct";
        freakingmonths[10] = "nov";
        freakingmonths[11] = "dec";
    
        var linkmonthnum = linktime.getMonth();
        var linkmonth = freakingmonths[linkmonthnum];
        var linkyear = linktime.getFullYear();
        var linkhour = linktime.getHours();
        var linkminute = linktime.getMinutes();
    
        if (linkminute < 10)
        {
            linkminute = "0" + linkminute;
        }
    
        var fomratedtime = linkday + linkmonth + linkyear + " " +
                           linkhour + ":" + linkminute + "h";
        return fomratedtime;
    }
    

    Simply provide your times in Unix timestamp format to this function; JavaScript already knows the timezone of the user.

    Like this:

    PHP:

    echo '';
    

    This will always show the times correctly based on the timezone the person has set on his/her computer clock. There is no need to ask anything to anyone and save it into places, thank god!

提交回复
热议问题