Javascript+php implements a method of displaying local time according to user time zone _php tips

Source: Internet
Author: User
Tags gmt time zone time zones local time set cookie setcookie

The example in this article describes how the javascript+php implementation displays local time based on user time zones. Share to everyone for your reference. Specifically as follows:

The following code is used across time zone applications, which is a piece of code previously written.

The server saves the related time configuration, the Save form is GMT time, the client needs according to the customer time zone to make the corresponding display, conforms to the customer custom.

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 is date = Exp.format (' yyyy-mm-dd HH:mm:ss ');
    if (indaylighttime (date)) {Setcookie (' indaylighttime ', 1, 1); }//Set cookie function Setcookie (c_name,value,expiredays) {var exdate=new Date () exdate.setdate (exdate.getdate () + Expiredays) document.cookie=c_name+ "=" +escape (value) + ((expiredays==null)? "" : "; Expires= "+exdate.togmtstring ())}///Judgment 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 time difference between the beginning and the year 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:

Conversion
function gmt_to_local ($time = ', $DST = FALSE) of GMT to local time within 24 hours {
  //javascript set cookie,php fetch
  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 will help you with your PHP program design.

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.