I'm using a javascript-based countdown timer that currently uses the client's system time to calculate the countdown. I assume that this line of code is where this is performed:
Code:
var nowTime = new Date();
I'm trying to make the script use server time instead as some people may have wrong dates/times set or live in different time zones etc.
So what I did was use a bit of php:
PHP Code:
var currentTime = <?php echo time(); ?>
var nowTime = new Date( currentTime);
This gives a result but now the countdown is 30 days off. I am testing on my local xampp server (which also uses system time) so there should be no discrepancy. I also tried adding
Code:
currentTime = currentTime – 2592000;
but no difference.
I can post the whole script if required but it is reasonably long.
Also shouldn't the php date be passed as a string so it should be
PHP Code:
var nowTime = new Date("<?php echo date("Y,n,j,G:i:s:u");?>");
No, because the JavaScript Date() object needs all the arguments to be delimited by commas, not by colons. I must have done myself the error in my post, but I have corrected later
I see where you are coming from but the various resources I have consulted say that the javascript date object also accepts a date string (which is what I'm using):
There are four ways of instantiating a date:
Code:
new Date() // current date and time
new Date(milliseconds) //milliseconds since 1970/01/01
new Date(dateString)
new Date(year, month, day, hours, minutes, seconds, milliseconds)
I see where you are coming from but the various resources I have consulted say that the javascript date object also accepts a date string (which is what I'm using):
Yes. Date() accepts 3 ways of writing arguments (the 4th - no arguments at all - returns the now date):
Code:
Date(milliseconds) // as a number
Date(dateString) // as a string
Date(year, month, day, hours, minutes, seconds, milliseconds) // as numbers, January is 0
dateString can be written as:
Code:
"MonthLongName date, year hours:minutes:seconds:milliseconds"
//or in American style
"month/date/year" // numbers within string, but this time January is 1
Depending to the JavaScript desired format, you may arrange your PHP variables accordingly. The most common format used by the coders is the "mathematical" one:
Code:
Date(year, month, day, hours, minutes, seconds, milliseconds) // as numbers, January is 0
Bookmarks