Can I use a php variable on javascript?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • londres9b
    New Member
    • Apr 2010
    • 106

    Can I use a php variable on javascript?

    I know very little about Javascript..

    It's for a countdown script.

    As far as I can tell, the javascript gets the 'today date and time' somehow (from the user's system).

    But I want to modify it so that it gets the server time (using php date).
    So I'll need to create a php variable and then add it to the javascript.(?) How do I do this?

    here's some of the script:

    Code:
    function countdown(yr, mo, da, deadlineHr, minute){
    
    var montharray = new Array("ene","feb","mar","abr","may","jun","jul","ago","sep","oct","nov","dic");
    //deportes should be passing in UTC times, daylight savings not accurate for non-us
    var UTCDeadlineHr = deadlineHr;
    var UTCMilliseconds = 1000 * 60 * 60 * UTCDeadlineHr;
    var today = new Date();
    var todayy = today.getUTCFullYear();
    if (todayy < 1000)
    todayy+=1900;
    var todaym = today.getUTCMonth();
    var todayd = today.getUTCDate();
    var todayh = today.getUTCHours();
    var todaymin = today.getUTCMinutes();
    var todaysec = today.getUTCSeconds();
    
    td= new Date(Date.UTC(todayy, todaym, todayd, todayh, todaymin, todaysec));
    
    
    endDate = new Date(Date.UTC(yr, mo-1, da, deadlineHr, minute, 0));
    
    var dd = (endDate - td);
    Sou you can see that td is the current date (today) and that endDate is the target date we are counting down to. The var. dd is the result between the subtraction of endDate by td, which gives us the time left.
  • ddsuresh
    New Member
    • Jun 2010
    • 5

    #2
    Code:
    <script type="text/javascript">
    var currenttime = '<? echo date("F d, Y H:i:s", time()) ?>';
    var serverdate = new Date(currenttime);
    </script>
    The above code will get the server date time and parse it into javascript date and time.

    Comment

    • londres9b
      New Member
      • Apr 2010
      • 106

      #3
      Thanks!!

      So what do I do, after inserting that code onto the script I do something like this ?

      Code:
      td= var serverdate;
      As I said, I don't know nothing about Javascript..
      if you could help with this..

      Comment

      • ddsuresh
        New Member
        • Jun 2010
        • 5

        #4
        Code:
        function countdown(yr, mo, da, deadlineHr, minute){
         
        var montharray = new Array("ene","feb","mar","abr","may","jun","jul","ago","sep","oct","nov","dic");
        //deportes should be passing in UTC times, daylight savings not accurate for non-us
        var UTCDeadlineHr = deadlineHr;
        var UTCMilliseconds = 1000 * 60 * 60 * UTCDeadlineHr;
        var today = new Date();
        var todayy = today.getUTCFullYear();
        if (todayy < 1000)
        todayy+=1900;
        var todaym = today.getUTCMonth();
        var todayd = today.getUTCDate();
        var todayh = today.getUTCHours();
        var todaymin = today.getUTCMinutes();
        var todaysec = today.getUTCSeconds();
        
        var currenttime = '<? echo date("F d, Y H:i:s", time()) ?>';
        var serverdate = new Date(currenttime);
         
        td= serverdate; 
         
        endDate = new Date(Date.UTC(yr, mo-1, da, deadlineHr, minute, 0));
         
        var dd = (endDate - td);

        I have updated your code, this might work for you.

        Comment

        • londres9b
          New Member
          • Apr 2010
          • 106

          #5
          I appreciate your help but no...
          It didn't work..

          All I got was NaN:NaN:NaN:NaN

          If you still want to help me, here is the page source code:

          Code:
          <html>
          <body>
          <span id="dys"></span>:<span id="hrs"></span>:<span id="min"></span>:<span id="sec"></span>
          
          <script type="text/javascript" src="countdownClock.js"></script>
          <script language="javascript">
          	// yr, mo, day, hr (24 hr format)
          	countdown(2010, 7, 19, 21, 9, true)
          </script>
          </body>
          </html>
          And here is the countdownClock. js , already modified with the code you provided.
          countdownClock.txt

          Comment

          Working...