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.