Javascript+php implements the method of displaying local time according to user time zone, JavaScript local _php tutorial

Source: Internet
Author: User
Tags gmt time zone set cookie

Javascript+php implements the method of displaying local time based on the user's time zone, JavaScript local


This article describes the Javascript+php implementation method that displays local time based on the user's time zone. Share to everyone for your reference. Specific as follows:

The following code is used in a cross-time zone application, which is a previously written piece of code.

The server saves the relevant time configuration, save in the form of GMT time, the client needs to be displayed according to the customer's time zone, in order to conform to customer habits.

1. The JavaScript code is as follows:

Window.onload = function () {//TODO begin processing Login user Time zone//Get login User Time zone and GMT time zone difference var exp = new Date ();    var gmthours =-(Exp.gettimezoneoffset ()/60);    Setcookie (' Customer_timezone ', gmthours,1);    Determine if daylight saving time date = Exp.format (' yyyy-mm-dd HH:mm:ss ');    if (indaylighttime (date)) {Setcookie (' indaylighttime ', 1, 1); }}//set cookiefunction Setcookie (c_name,value,expiredays) {var exdate=new Date () exdate.setdate (Exdate.getdate () +expi Redays) document.cookie=c_name+ "=" +escape (value) + ((expiredays==null)? "" : "; Expires= "+exdate.togmtstring ())}//Determine whether the time is the Eastern hemisphere or the hemispheric function Iseastearthtime (newdate) {var dj= newdate.getgmtoffset (  FALSE);  if (Dj.indexof ("-") = =-1) {return true;  } else {return false; }}//is daylight saving time function Indaylighttime (date) {var start = new Date (Date.gettime ()); Start.setmonth (0); start.setdate (1); Start.sethours (0); Start.setminutes (0); Start.setseconds (0); var middle = new Date (Start.gettime ()); Middle.setmonth (6); If the year starts and the mid-year time difference is the same, it is considered that there is no daylight saving time if (middle.getTimezoneOffset ()-start.gettimezoneoffset ()) = = 0) {return false;} var margin = 0;  if (this.iseastearthtime (date)) {margin = Middle.gettimezoneoffset ();} else {margin = Start.gettimezoneoffset ();} if (date.gettimezoneoffset () = = margin) {return true;} return false;} Done End

2. Server-side PHP code:

24 hours GMT to local time conversion function gmt_to_local ($time = ", $dst = FALSE) {  //javascript set cookie,php value  if (Isset ($_ cookie["Customer_timezone"]) {    $timezone = $_cookie["Customer_timezone"];  } else{    $timezone = 0;  }  if ($time = = ") {    return now ();  }  Time processing  $time + = $timezone * 3600;     is daylight saving time  if (Isset ($_cookie["Indaylighttime") && $_cookie["Indaylighttime"]==1)  {    $DST = TRUE;  }  if ($dst = = TRUE) {    $time + = 3600;  }  Return date ("H:i", $time);}

I hope this article is helpful to everyone's PHP programming.

http://www.bkjia.com/PHPjc/966546.html www.bkjia.com true http://www.bkjia.com/PHPjc/966546.html techarticle javascript+php implements the method of displaying local time based on the user's time zone, JavaScript local examples of this article describe how javascript+php implements local time based on the user's time zone. Share to ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.