Jump to content

get date for one time zone


westman

Recommended Posts

The Date() object uses the time local to the user's computer. There are no time zones since you can't guess where the client's computer is. If you want to print the time in London, you'll need to use the server time with a server-side language.

Link to comment
Share on other sites

No, PHP and Javascript are completely independent languages. You can't just pass PHP date information to Javascript. Javascript's Date object uses the time that's on the client's computer, independently of the time zone. You can use Javascript's setDate(), setHours(), setMinutes() ... in conjunction with PHP's date like this:

<?php date_default_timezone_set('Europe/London'); ?>var d = new Date();d.setYear(<?php echo date('Y'); ?>);d.setMonth(<?php echo date('m'); ?>);d.setDate(<?php echo date('d'); ?>);d.setHour(<?php echo date('h'); ?>);d.setMinutes(<?php echo idate('i'); ?>);

Link to comment
Share on other sites

Since we are in this topic I have a question regarding time with JavaScript. In one of my latest projects i had to display the time in a specific country, so i used the getUTCHours/Minutes/Seconds functions and added some hours accordingly to the values returned. Is this a good practice?

Link to comment
Share on other sites

ok so if i use...

<?php date_default_timezone_set('Europe/London'); ?><script>var before="Monday!"var current="Today is Monday!"var montharray=new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec")function countdown(yr,m,d){theyear=yr;themonth=m;theday=dvar today=new Date()var todayy=today.getYear()if (todayy < 1000)todayy+=1900var todaysec=today.setSeconds(<?php echo date('s'); ?>);var todaym=today.setMonth(<?php echo date('m'); ?>);var todayd=today.setDate(<?php echo date('d'); ?>);var todayh=today.setHour(<?php echo date('h'); ?>);var todaymin=today.setMinutes(<?php echo idate('i'); ?>);var todaystring=montharray[todaym]+" "+todayd+", "+todayy+" "+todayh+":"+todaymin+":"+todaysecfuturestring=montharray[m-1]+" "+d+", "+yrdd=Date.parse(futurestring)-Date.parse(todaystring)dday=Math.floor(dd/(60*60*1000*24)*1)dhour=Math.floor((dd%(60*60*1000*24))/(60*60*1000)*1)dmin=Math.floor(((dd%(60*60*1000*24))%(60*60*1000))/(60*1000)*1)dsec=Math.floor((((dd%(60*60*1000*24))%(60*60*1000))%(60*1000))/1000*1)if(dday==0&&dhour==0&&dmin==0&&dsec==1){document.forms.count.count2.value=currentreturn}elsedocument.getElementById('countdown').textContent ="Only "+dday+ " days, "+dhour+" hours, "+dmin+" minutes, and "+dsec+" seconds left until "+before<!--document.forms.count.count2.value="Only "+dday+ " days, "+dhour+" hours, "+dmin+" minutes, and "+dsec+" seconds left until "+before-->setTimeout("countdown(theyear,themonth,theday)",1000)}//enter the count down date using the format year/month/day//countdown(2012,12,25)  countdown(<?php echo $nextMonday; ?>)</script>

...will this work?

Link to comment
Share on other sites

am still having problems geting java script to read the date of 1 time zone any help?

<div id="count"><?phpdate_default_timezone_set('Europe/London');$day = date('d');$month = date('m');$year = date('Y');$nextMonday = date("Y, m, d",strtotime('next Monday',mktime(0,0,0,$month,$day,$year)));echo $nextMonday;?></div>	   	  <!--<form name="count"><input type="text" size="69" name="count2"></form>--><div id="countdown"></div><script>var before="Monday in London!"var current="Today is Monday!"var montharray=new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec")function countdown(yr,m,d){theyear=yr;themonth=m;theday=dvar today=new Date()var todayy=today.getYear()if (todayy < 1000)todayy+=1900var todaym=today.getMonth()var todayd=today.getDate()var todayh=today.getHours()var todaymin=today.getMinutes()var todaysec=today.getSeconds()var todaystring=montharray[todaym]+" "+todayd+", "+todayy+" "+todayh+":"+todaymin+":"+todaysecfuturestring=montharray[m-1]+" "+d+", "+yrdd=Date.parse(futurestring)-Date.parse(todaystring)dday=Math.floor(dd/(60*60*1000*24)*1)dhour=Math.floor((dd%(60*60*1000*24))/(60*60*1000)*1)dmin=Math.floor(((dd%(60*60*1000*24))%(60*60*1000))/(60*1000)*1)dsec=Math.floor((((dd%(60*60*1000*24))%(60*60*1000))%(60*1000))/1000*1)if(dday==0&&dhour==0&&dmin==0&&dsec==1){document.forms.count.count2.value=currentreturn}elsedocument.getElementById('countdown').textContent ="Only "+dday+ " days, "+dhour+" hours, "+dmin+" minutes, and "+dsec+" seconds left until "+before<!--document.forms.count.count2.value="Only "+dday+ " days, "+dhour+" hours, "+dmin+" minutes, and "+dsec+" seconds left until "+before-->setTimeout("countdown(theyear,themonth,theday)",1000)}//enter the count down date using the format year/month/day//countdown(2012,12,25)  countdown(<?php echo $nextMonday; ?>)</script>

the idea is for java script to not read the date and time of a users pc and give a count down to monday in London,UK

Link to comment
Share on other sites

Use PHP to get the timestamp for monday and the current timestamp, subtract to get the difference and echo that as a Javascript variable, and then use Javascript to do the countdown based on the difference from PHP. Keep in mind that timestamps from PHP are given in seconds, and Javascript uses timestamps in milliseconds.

Link to comment
Share on other sites

i know that but how do i get it to echo out in the top part of my script?

var today=new Date()var todayy=today.getYear()if (todayy < 1000)todayy+=1900var todaym=today.getMonth()var todayd=today.getDate()var todayh=today.getHours()var todaymin=today.getMinutes()var todaysec=today.getSeconds()

the full script is in post NO. 9

Link to comment
Share on other sites

yes but in...

var today=new Date()var todayy=today.getYear()if (todayy < 1000)todayy+=1900var todaym=today.getMonth()var todayd=today.getDate()var todayh=today.getHours()var todaymin=today.getMinutes()var todaysec=today.getSeconds()

i cant echo out normaly coz my script will not work. so how do i do it the right way? am trying to get the php time in javascript, please look at post 9.

Link to comment
Share on other sites

You should be using getFullYear() instead of getYear() What you really need to do is find out the difference between the server time and the user's time, then make calculations based on that.

var today = new Date();var serverTime = <?php echo time() * 1000; ?>var clientTime = today.getTime();var difference = serverTime - clientTime;

Use the difference to calculate what client date you should count down towards.

Link to comment
Share on other sites

Duotone Fox i like the look and feel of your code in post NO. 17 but i tryd it and i could not get it working.am stuck.am not to good at this, i can only try code i see am not good at creating from you explaning. could some one please give example code, it would be helpful.thank you.

Link to comment
Share on other sites

Do you know how to look at the error console? In Firefox the shortcut is Control+Shift+J. Show us the code you have and maybe we'll be able to point out the problem.

Link to comment
Share on other sites

i could not find any errors in the browserbut i did get this... Only NaN days, NaN hours, NaN minutes, and NaN seconds left until Monday in London! when i used...

var todayy=today.setYear(<?php echo date('Y'); ?>);if (todayy < 1000)todayy+=1900var todaym=today.setMonth(<?php echo date('m'); ?>);var todayd=today.setDate(<?php echo date('d'); ?>);var todayh=today.setHours(<?php echo date('h'); ?>);var todaymin=today.setMinutes(<?php echo idate('i'); ?>);var todaysec=today.getSeconds()

where do i go from here?

Link to comment
Share on other sites

setMonth() doesn't return any values. Use getMonth() to get the value. It's Get with a G.

/* Setting the date object */today.setMonth(<?php echo date('m'); ?>);today.setDate(<?php echo date('d'); ?>);today.setHours(<?php echo date('h'); ?>);today.setMinutes(<?php echo idate('i'); ?>); /* Getting values from the object */var todaym=today.getMonth();var todayd=today.getDate();var todayh=today.getHours();var todaymin=today.getMinutes();var todaysec=today.getSeconds()

Link to comment
Share on other sites

its still not working for me :( i got this script by looking in google, a simple count down script that takes the time from the users pc. dose anyone no any simple count down scripts that dose not use the time from the users pc, and uses 1 time from 1 time zone?this would be more helpful than trying to changing me script from geting the users time to 1 time zone. if any 1 knows any good scripts please post links, and thank you all for your time realy thankful ;)

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...