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
Javascript + php shows the local time according to the user's time zone. Javascript + php shows the local time based on the user's time zone. This section describes how to display the local time based on the user's time zone using javascript + php. Share with javascript + php to display the local time according to the user's time zone.

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 () + Expiredays) 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.

Examples in this article describes how to display the local time in javascript + php based on the user's time zone. Share...

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.