Jump to content

Javascript Date/Time manipulation on different locales and OSs


stakisko

Recommended Posts

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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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'.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

Just testing ;D

 

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

 

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.