0

My problem is as follows: I want to display nepalese standard time in my website,so i set default timezone of my website to 'Asia/kathmandu' using command: php_value date.timezone 'Asia/kathmandu' in htaccess file.

when i display time using any php functions like strftime() or date() ,it shows the nepalese standard time, But when i use javascript function new Date(<?php echo time()*1000; ?>),it displays the time of my personal pc i am using to view my website. How can i display correct time using javascript date functions? Can anybody help me out?

asked May 27, 2013 at 4:22
3
  • Javascript will always show your system date. if you need server time then you need to get the date or time from the server. Commented May 27, 2013 at 4:35
  • stackoverflow.com/questions/439630/… Commented May 27, 2013 at 4:35
  • @HarryBomrah—javascript Dates use a UTC time value that, by default, is based on a combination of the system settings for the timezone offset and system's current time. Commented May 27, 2013 at 5:51

3 Answers 3

2

Your issue is because javascript (actually ECMAScript) date objects are based on a UTC time value. When you do:

new Date(<?php echo time()*1000; ?>)

you are passing a UTC millisecond time value to the Date constructor, which then creates a date object. When you use the usual Date methods to format a string, or use Date.prototpye.toString or Date.prototype.toLocaleString, you will get a string based on the client's locale. Note that all these strings are implementation dependent and vary widely for the locale version.

If you want the timezone of the server, then use the server to set it. Or you can send a time zone offset in minutes to be applied to the local time to get back to Nepalese Standard Time (UTC + 5:45). Note that in ECMAScript, the time zone offset is minutes to be added to the local time to get UTC, whereas it is more normal to define the offset in minutes to be added to UTC to get the local time.

So to get NST:

function toNST(timeValue) {
 function z(n) {return (n<10? '0' : '') + n}
 var d = new Date();
 var nstOffset = 5 * 60 + 45;
 d.setMinutes(d.getMinutes() + d.getTimezoneOffset() + nstOffset);
 return z(d.getHours()) + ':' + z(d.getMinutes()) + ':' + z(d.getSeconds());
}
alert(toNST(+(new Date()))); // about 11:07:17 at the moment
answered May 27, 2013 at 5:22
Sign up to request clarification or add additional context in comments.

Comments

0

Use

new Date(Date.NPT(year, month, day, hour, minute, second))
answered May 27, 2013 at 4:55

Comments

0

Call the time via ajax from your server. That has the advantage of a better code maintanance. If you change the time again (e.g. if you want to use the code for another location) you have only to change the time in .haccess.

answered May 27, 2013 at 4:58

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.