Javascript+php implementing methods for displaying local time based on user Time zone _php tutorial

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

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


This article mainly introduces the javascript+php implementation of the local time according to the user time zone method, instance analysis JavaScript to obtain the client time zone and the server-side PHP interaction skills, with a certain reference value, the need for friends can refer to the next

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:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21st

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

Window.onload = function () {

TODO begin processing Login user Time zone

Get the difference between the login user time zone and GMT time zone

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 cookies

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 ())

}

Judging whether the time is the old or the Western Hemisphere

function Iseastearthtime (newdate)

{

var dj= newdate.getgmtoffset (false);

if (Dj.indexof ("-") = =-1) {

return true;

} else {

return false;

}

}

Whether it 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 middle time difference is the same, it is considered that the country does not have daylight

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:

?

1

2

3

4

5

6

7

8

9

Ten

One

+

All

+

+

2 0

+

24

///+ convert GMT to local time

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/966930.html www.bkjia.com true http://www.bkjia.com/PHPjc/966930.html techarticle javascript+php Implementation method to display local time according to user's time zone this article mainly introduces the javascript+php implementation of the local time according to the user time zone method, instance analysis Javascrip ...

  • 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.