How to display the local time according to the user's time zone using javascript + php

Source: Internet
Author: User
Tags gmt time zone set cookie
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.

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.

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:

Window. onload = function () {// TODO begin processes the login user's Time Zone // obtains the difference between the login user's time zone and the GMT Time Zone var exp = new Date (); var gmtHours =-(exp. getTimezoneOffset ()/60); setCookie ('customer _ timezone ', gmtHours, 1); // date = exp when determining whether the value is timeout. 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 () + expireda Ys) document. cookie = c_name + "=" + escape (value) + (expiredays = null )? "": "; Expires =" + exdate. toGMTString ()} // determines whether the time is in 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 the function inDaylightTime (date) is used for callback) {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 year and the year is the same, if (middle. getTimezoneOffset ()-start. getTimezoneOffset () = 0) {return false;} var margin = 0; if (this. iseasteartime (date) {margin = middle. getTimezoneOffset ();} else {margin = start. getTimezoneOffset ();} if (date. getTimezoneOffset () = margin) {return true;} return false;} // DONE end

2. server-side php code:

// Function gmt_to_local ($ time = '', $ dst = FALSE) for converting GMT to local time within 24 hours {// set Cookie in JavaScript, PHP value: if (isset ($ _ COOKIE ["customer_timezone"]) {$ timezone = $ _ COOKIE ["customer_timezone"] ;}else {$ timezone = 0 ;} if ($ time = '') {return now () ;}// time processing $ time + = $ timezone * 3600; // 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 php programming.

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.