Javascript + php: how to display the local time according to the user's time zone _ PHP Tutorial

Source: Internet
Author: User
Tags gmt time zone
Javascript + php shows the local time based on the user's time zone. Javascript + php: how to display local time based on the user's time zone this article mainly introduces how to display local time based on the user's time zone in javascript + php, instance analysis using javascrip javascript + php to display local time based on the user's time zone

This article mainly introduces how to display the local time in javascript + php based on the user's time zone. It provides some reference for examples to analyze how javascript can obtain the client time zone and interact with php on the server, for more information, see

This example describes how to display the local time in javascript + php based on the user's time zone. Share it with you for your reference. The details are as follows:

The following code is used in cross-time zone applications. this is a piece of code previously written.

The server saves the relevant time configuration in the form of GMT. the client needs to display the time according to the customer's time zone to meet the customer's 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

21

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 processes the time zone of the login user

// Obtain the difference between the time zone of the login user and the GMT time zone

Var exp = new Date ();

Var gmtHours =-(exp. getTimezoneOffset ()/60 );

SetCookie ('customer _ timezone ', gmtHours, 1 );

// When determining whether it is seeking

Date = exp. format ('yyyy-MM-dd HH: mm: SS ');

If (inDaylightTime (date )){

SetCookie ('daylighttime', 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 ())

}

// Determine whether the time is the eastern hemisphere or the Western Hemisphere

Function iseasteartime (newDate)

{

Var dj = newDate. getGMTOffset (false );

If (dj. indexOf ("-") =-1 ){

Return true;

} Else {

Return false;

}

}

// Whether it is enabled

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 is the same from the beginning of the year and the middle of the year, it is deemed that there is no renewal time in this country

If (middle. getTimezoneOffset ()-start. getTimezoneOffset () = 0)

{

Return false;

}

Var margin = 0;

If (this. iseastearthur time (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

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

// Convert GMT to local time within 24 hours

Function gmt_to_local ($ time = '', $ dst = FALSE ){

// Set the Cookie in JavaScript and the PHP value

If (isset ($ _ COOKIE ["customer_timezone"]) {

$ Timezone = $ _ COOKIE ["customer_timezone"];

} Else {

$ Timezone = 0;

}

If ($ time = ''){

Return now ();

}

// Time processing

$ Time + = $ timezone * 3600;

// If it is enabled

If (isset ($ _ COOKIE ["inDaylightTime"]) & $ _ COOKIE ["inDaylightTime"] = 1)

{

$ Dst = TRUE;

}

If ($ dst = TRUE ){

$ Time plus = 3600;

}

Return date ("H: I", $ time );

}

I hope this article will help you with php programming.

This article introduces how to display the local time in javascript + php based on the user's time zone...

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.