stakisko Posted June 29, 2011 Share Posted June 29, 2011 Heya, so i am trying to make a countdown timer between now and a date i got from PHP. (Using ZF but i thougt it would be best to write it here since is Javascript related). The problem is that i can never get the timer work on both my Debian box and my Windows 7 box since there are differences on locales and the os's languages. Here is the code from me view: setInterval(function(){ var laterDate = new Date(<?php echo $this->time ?>); var earlierDate = new Date(); //document.write(laterDate.toTimeString()); var nTotalDiff = laterDate.getTime() - earlierDate.getTime(); var oDiff = new Object(); oDiff.days = Math.floor(nTotalDiff/1000/60/60/24); nTotalDiff -= oDiff.days*1000*60*60*24; oDiff.hours = Math.floor(nTotalDiff/1000/60/60); nTotalDiff -= oDiff.hours*1000*60*60; oDiff.minutes = Math.floor(nTotalDiff/1000/60); nTotalDiff -= oDiff.minutes*1000*60; oDiff.seconds = Math.floor(nTotalDiff/1000); if(oDiff.days > 0) { oDiff.hours += oDiff.days*24; } if(oDiff.hours==0 && oDiff.minutes==0 && oDiff.seconds==0){ var url = 'http://localhost/eimaimesa/public/index/disable/offer/' + <?php echo $this->offerID; ?>; $.post(url, function(data) { location.reload(); }); } document.getElementById('md_stime').innerHTML = oDiff.hours + ':' + oDiff.minutes + ':' + oDiff.seconds; }, 1000); and this is from the controller $date = new Zend_Date($this->view->mainOffer->getEndDate()); $this->view->time = $date->getTimeStamp(); I tried different aproaches to get the Date from the controller, using simple string or the timestamp to pass it to Javascript. I need to give the dates but only get the difference in hours, minutes, seconds for my timer? What should i consinder to make this Javascript code cross locale and language? Thank you in advance Greeting from Greece, Kostas Quote Link to comment https://forums.phpfreaks.com/topic/240680-javascript-datetime-manipulation-on-different-locales-and-oss/ Share on other sites More sharing options...
Adam Posted June 29, 2011 Share Posted June 29, 2011 What's the actual problem? Quote Link to comment https://forums.phpfreaks.com/topic/240680-javascript-datetime-manipulation-on-different-locales-and-oss/#findComment-1236223 Share on other sites More sharing options...
stakisko Posted June 29, 2011 Author Share Posted June 29, 2011 The problem is that i need to get the time somehow to work independently of the language. You see the second controller approach in the controller i get the Date formatted like 'Jan 1, 2011' but a different language os cant understand 'Jan' so it doesn't compute the difference of the time. How to pass the date so any os of any languange can understand? Quote Link to comment https://forums.phpfreaks.com/topic/240680-javascript-datetime-manipulation-on-different-locales-and-oss/#findComment-1236227 Share on other sites More sharing options...
Adam Posted June 29, 2011 Share Posted June 29, 2011 Ah, use a Unix timestamp. Basically the milliseconds since 01/01/1970, which both PHP and JS understand regardless of OS. Quote Link to comment https://forums.phpfreaks.com/topic/240680-javascript-datetime-manipulation-on-different-locales-and-oss/#findComment-1236231 Share on other sites More sharing options...
stakisko Posted June 29, 2011 Author Share Posted June 29, 2011 I already tried my friend, and still the problem exists! As you can see in the above code this is what i am doing but still.. Could you see where is the problem in the above code? What i am making wrong? Quote Link to comment https://forums.phpfreaks.com/topic/240680-javascript-datetime-manipulation-on-different-locales-and-oss/#findComment-1236233 Share on other sites More sharing options...
Adam Posted June 29, 2011 Share Posted June 29, 2011 Do you have an example available online? Would make debugging a lot easier. Quote Link to comment https://forums.phpfreaks.com/topic/240680-javascript-datetime-manipulation-on-different-locales-and-oss/#findComment-1236238 Share on other sites More sharing options...
stakisko Posted June 29, 2011 Author Share Posted June 29, 2011 No unfortunately. Guide me and i ll take it where you want... Quote Link to comment https://forums.phpfreaks.com/topic/240680-javascript-datetime-manipulation-on-different-locales-and-oss/#findComment-1236240 Share on other sites More sharing options...
Adam Posted June 29, 2011 Share Posted June 29, 2011 If you view the source when the code is running, what's the argument passed to the Date object (in an environment where it doesn't work)? Quote Link to comment https://forums.phpfreaks.com/topic/240680-javascript-datetime-manipulation-on-different-locales-and-oss/#findComment-1236245 Share on other sites More sharing options...
stakisko Posted June 29, 2011 Author Share Posted June 29, 2011 Passing the timestamp the code is ok but the problem is afterwards in the difference of the two dates. Passing the date as a string from the controller the problem is that the different os cant understand the word of the month. So gives out an 'Invalid Date'. Quote Link to comment https://forums.phpfreaks.com/topic/240680-javascript-datetime-manipulation-on-different-locales-and-oss/#findComment-1236248 Share on other sites More sharing options...
Adam Posted June 29, 2011 Share Posted June 29, 2011 I'm having a little trouble understanding some of the things you're saying. "Passing the timestamp the code" - I'm guessing you mean passing the code the timestamp, but what code are you referring to? At what point in this process (which call, which language, etc) is the failure happening? Quote Link to comment https://forums.phpfreaks.com/topic/240680-javascript-datetime-manipulation-on-different-locales-and-oss/#findComment-1236251 Share on other sites More sharing options...
stakisko Posted June 29, 2011 Author Share Posted June 29, 2011 ok really sorry for the misunderstanding. so when i say passing the timestamp to the code i mean this snippet new Date(<?php echo $this->time ?>); where i am trying to pass a date taken from mysql in the form of 'dd-mm-yyyy hh:mm:ss' to Javascript. Then i will remove it from the current date and show the difference in 'hh:mm:ss' format. so passing the timestamp of the mysqldata doesn't work, and also passing the date as string also doesn't work. i wrote what is happening in both situation above. tell me if i am missing anything. thank you again for your time Quote Link to comment https://forums.phpfreaks.com/topic/240680-javascript-datetime-manipulation-on-different-locales-and-oss/#findComment-1236258 Share on other sites More sharing options...
Adam Posted June 29, 2011 Share Posted June 29, 2011 JavaScript doesn't understand a MySQL time stamp. As I was suggesting, pass JavaScript a Unix time stamp (integer value e.g. 1309388400) that cuts out any language barriers. Quote Link to comment https://forums.phpfreaks.com/topic/240680-javascript-datetime-manipulation-on-different-locales-and-oss/#findComment-1236265 Share on other sites More sharing options...
stakisko Posted June 29, 2011 Author Share Posted June 29, 2011 $date = new Zend_Date(/*date from mysql as string*/); $this->view->time = $date->getTimeStamp(); isn't this a Unix time stamp? Quote Link to comment https://forums.phpfreaks.com/topic/240680-javascript-datetime-manipulation-on-different-locales-and-oss/#findComment-1236273 Share on other sites More sharing options...
Adam Posted June 29, 2011 Share Posted June 29, 2011 Just testing So back to my other request, can you show what is being passed to the date object by looking at the source when running the code? Need the actual value, not an explanation though. Quote Link to comment https://forums.phpfreaks.com/topic/240680-javascript-datetime-manipulation-on-different-locales-and-oss/#findComment-1236277 Share on other sites More sharing options...
stakisko Posted June 29, 2011 Author Share Posted June 29, 2011 var laterDate = new Date(1309291823); thats the output of the source code at run time. and now i am taking the same result of the removal of the two dates what ever date i put in. At this point i assume that Javascript removal is not working properly. But why? if i do this in the controller $this->view->time = new Zend_Date($this->view->mainOffer->getEndDate()); and then var laterDate = new Date(<?php echo '\'' . $this->time . '\''; ?>); then the output is var laterDate = new Date(Jun 28, 2011 11:10:23 PM); the second wont work on a different language os. It gives a NaN:NaN:NaN. Quote Link to comment https://forums.phpfreaks.com/topic/240680-javascript-datetime-manipulation-on-different-locales-and-oss/#findComment-1236298 Share on other sites More sharing options...
Adam Posted June 29, 2011 Share Posted June 29, 2011 Right I'm confused. var laterDate = new Date(1309291823); Does this work (forget passing it a string for now)? If not, do you receive an error? Do you see any output ("Nan:Nan[...]" for example)? What does happen? If you run this as a static value (i.e. replace the dynamic PHP echo with it) does it work then? I've ran the JS with a Unix time stamp myself and it works; and should always work regardless of language given it's an integer. Quote Link to comment https://forums.phpfreaks.com/topic/240680-javascript-datetime-manipulation-on-different-locales-and-oss/#findComment-1236323 Share on other sites More sharing options...
stakisko Posted June 29, 2011 Author Share Posted June 29, 2011 ok... if i do a document.write(laterDate) it gives me another date and not the actual date of the timestamp i passed. The date i passed from php was at 2011 year but Javascript shows me a 1970 year. But the time is shown properly, no NaN etc. Doing a quick online conversion of the unix timestamp it gives me the correct date. Maybe i need to do a conversion in Javascript using right offsets of the TimeZone? Would this be an issue? Quote Link to comment https://forums.phpfreaks.com/topic/240680-javascript-datetime-manipulation-on-different-locales-and-oss/#findComment-1236330 Share on other sites More sharing options...
stakisko Posted June 29, 2011 Author Share Posted June 29, 2011 ok my friend i found this // create a new javascript Date object based on the timestamp // multiplied by 1000 so that the argument is in milliseconds, not seconds var date = new Date(unix_timestamp*1000); where it clearly approves that Date() javascript function needs the timestamp in milliseconds but Unix Timestamp is in seconds. So multiplied by 1000 would give the correct date on Javascript. That was the problem after all. Thank you for your time MrAdam. Take care Quote Link to comment https://forums.phpfreaks.com/topic/240680-javascript-datetime-manipulation-on-different-locales-and-oss/#findComment-1236380 Share on other sites More sharing options...
Adam Posted June 29, 2011 Share Posted June 29, 2011 Ohh bloody hell. Silly oversight.. I even looked up the valid arguments to pass the constructor! Well, we both live and learn.. Quote Link to comment https://forums.phpfreaks.com/topic/240680-javascript-datetime-manipulation-on-different-locales-and-oss/#findComment-1236383 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.