westman Posted January 14, 2012 Share Posted January 14, 2012 how do i get this to work out the date dy the time in London,UK?var today=new Date()am guesing that the code will find out the time on a users pc and + or - some hours due to time zone Link to comment Share on other sites More sharing options...
Ingolme Posted January 14, 2012 Share Posted January 14, 2012 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 More sharing options...
westman Posted January 15, 2012 Author Share Posted January 15, 2012 what if i was to use... <?phpdate_default_timezone_set('Europe/London');?> <script> var today=new Date(<?php echo date_default_timezone_set ?>)</script> would this work to get the UK time in javascript? Link to comment Share on other sites More sharing options...
Ingolme Posted January 15, 2012 Share Posted January 15, 2012 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 More sharing options...
TheGallery Posted January 16, 2012 Share Posted January 16, 2012 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 More sharing options...
justsomeguy Posted January 17, 2012 Share Posted January 17, 2012 That's fine, but it relies on the user's clock being correct. Link to comment Share on other sites More sharing options...
westman Posted January 21, 2012 Author Share Posted January 21, 2012 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 More sharing options...
justsomeguy Posted January 23, 2012 Share Posted January 23, 2012 Run it and see if it does what you want. Keep an eye on the error console to check for errors even though it may look like it's running. Link to comment Share on other sites More sharing options...
westman Posted January 27, 2012 Author Share Posted January 27, 2012 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 More sharing options...
justsomeguy Posted January 27, 2012 Share Posted January 27, 2012 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 More sharing options...
westman Posted January 28, 2012 Author Share Posted January 28, 2012 i have the monday date echo'ed out in javascriptbut do not have the php time in javascript am not a pro like you, could you give a code example of geting the php date in javascript plaese Link to comment Share on other sites More sharing options...
justsomeguy Posted January 30, 2012 Share Posted January 30, 2012 You can use PHP to echo content anywhere, including inside Javascript. <script type="text/javascript"><?php $value = time();echo "var js_var = {$value};"; ?></script> Link to comment Share on other sites More sharing options...
westman Posted January 31, 2012 Author Share Posted January 31, 2012 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 More sharing options...
justsomeguy Posted January 31, 2012 Share Posted January 31, 2012 Like I said, you can use PHP to echo content anywhere you want to. Just add a PHP block wherever you want and output the value. var today = <?php echo time(); ?>; Link to comment Share on other sites More sharing options...
westman Posted January 31, 2012 Author Share Posted January 31, 2012 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 More sharing options...
justsomeguy Posted January 31, 2012 Share Posted January 31, 2012 Have you even tried what I've suggested, or are you just assuming that it's not going to work right? Run the code from post 14, or post 12, open it in a browser, view the source, and look at what it actually did. Link to comment Share on other sites More sharing options...
Ingolme Posted January 31, 2012 Share Posted January 31, 2012 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 More sharing options...
westman Posted January 31, 2012 Author Share Posted January 31, 2012 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 More sharing options...
justsomeguy Posted January 31, 2012 Share Posted January 31, 2012 All of this is example code. If it doesn't work, say what happens. If you get an error message, post the error message. Link to comment Share on other sites More sharing options...
westman Posted January 31, 2012 Author Share Posted January 31, 2012 am not geting error message, if it works i see ... Only 5 days, 2 hours, 55 minutes, and 55 seconds left until Monday! on the page in a browser and if it dose not work i will not see eneything Link to comment Share on other sites More sharing options...
Ingolme Posted February 1, 2012 Share Posted February 1, 2012 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 More sharing options...
justsomeguy Posted February 1, 2012 Share Posted February 1, 2012 View the source code in the browser, and check the console for Javascript errors. Link to comment Share on other sites More sharing options...
westman Posted February 2, 2012 Author Share Posted February 2, 2012 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 More sharing options...
Ingolme Posted February 2, 2012 Share Posted February 2, 2012 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 More sharing options...
westman Posted February 3, 2012 Author Share Posted February 3, 2012 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 More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.