www.webdeveloper.com
Results 1 to 10 of 10

Thread: Passing server time to javascript

  1. #1
    Join Date
    May 2009
    Location
    UK
    Posts
    39

    Passing server time to javascript

    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.

  2. #2
    Join Date
    May 2009
    Location
    UK
    Posts
    39
    Solved it!

    I passed the js date() function a date string rather than seconds:

    PHP Code:
    <?php date_default_timezone_set('Europe/London'); ?>
    <script type="text/javascript">var currentTime = "<?php echo date('F j Y h:i:s A'); ?>"

    var nowTime = new Date(currentTime);


    </script>

  3. #3
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    Wrong PHP approach. use this:
    Code:
    <?php date_default_timezone_set(the server's zone here); ?>
    var nowTime = new Date(<?php echo date("Y,n,j,G,i,s,u");?>);
    u - milliseconds - only starting from PHP 5
    Last edited by Kor; 02-04-2011 at 07:14 AM.

  4. #4
    Join Date
    May 2009
    Location
    UK
    Posts
    39
    Quote Originally Posted by Kor View Post
    Wrong PHP approach. use this:
    Code:
    var nowTime = new Date(<?php echo date("Y,n,j,G:i:s:u");?>);
    u - milliseconds - only starting from PHP 5
    I don't need the accuracy of milliseconds for my countdown. 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");?>");
    Also, is the way I've done it bad practice (because it works!)?

  5. #5
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    Quote Originally Posted by miocene View Post
    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

  6. #6
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    In fact I guess this would be enough, without specifying the time-zone:
    Code:
    var nowTime = new Date(<?php echo date("Y,n,j,G,i,s,u");?>);

  7. #7
    Join Date
    May 2009
    Location
    UK
    Posts
    39
    But my original method works (am using the timezone deliberately)

  8. #8
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    Quote Originally Posted by miocene View Post
    But my original method works (am using the timezone deliberately)
    There are many ways to skin a cat

  9. #9
    Join Date
    May 2009
    Location
    UK
    Posts
    39
    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)
    you can visit http://sergedenimes.co.uk to see if it works for you

  10. #10
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    Quote Originally Posted by miocene View Post
    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
    Last edited by Kor; 02-04-2011 at 08:34 AM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles