Results 1 to 4 of 4

Thread: Echoing time/date

  1. #1

    Thread Starter
    Addicted Member MoE70's Avatar
    Join Date
    May 2006
    Posts
    185

    Echoing time/date

    hi

    does anyone know how i might be able to echo a java script

    example

    chat box
    PHP Code:
    <?php echo $msg?><i> <script type="text/javascript">
    <!--
    var currentTime = new Date()
    var hours = currentTime.getHours()
    var minutes = currentTime.getMinutes()

    if (minutes < 10)
    minutes = "0" + minutes

    document.write(hours + ":" + minutes)
    if(hours > 11){
    document.write("pm")
    } else {
    document.write("am")
    }
    //-->
    </script></i>
    but i want to print th time after $msg

    i tried the normal php tags time() but it prints it without a format...just numbers

    i tried print date("D dS M,Y h:i a"); but that prints a different time zone. my hosting company's time zone perhaps
    Last edited by MoE70; Jun 4th, 2006 at 08:30 PM.

  2. #2
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Echoing time/date

    Use date_default_timezone_set() if on 5.1.x or later. Otherwise you're going to have to manually add the date difference on.

    Note that there's no way to get the user's timezone, you'd need JS for that.

  3. #3

    Thread Starter
    Addicted Member MoE70's Avatar
    Join Date
    May 2006
    Posts
    185

    Re: Echoing time/date

    nope unfortunately i got 4.3

    but isnt there a way to make a php variable to echo the jave script? like for example


    PHP Code:
    $time= "<?php echo $msg?><i> <script type="text/javascript">
    <!--
    var currentTime = new Date()
    var hours = currentTime.getHours()
    var minutes = currentTime.getMinutes()

    if (minutes < 10)
    minutes = "0" + minutes

    document.write(hours + ":" + minutes)
    if(hours > 11){
    document.write("pm")
    } else {
    document.write("am")
    }
    //-->
    </script></i>"
    PHP Code:
    <? php echo $time ?>
    btw i dont know php..

  4. #4
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Echoing time/date

    Haha. No, you can't run Javascript on the server.

    I say this a lot because I really believe in it: It's of utmost importance, if you are going to be doing any web development, to understand the client/server relationship. The communication between server and client takes place in one hit - request made from the client - response from the server. PHP processing is done on the server before sending the response. JS processing starts in the browser once the response, containing JS code, is received. So that's why we say PHP is a server-side language and JS is client-side.

    I'm sorry if you already knew any of that, but that's the reason why you can't run JS on the server (unless of course you had some sort of JS processor extension for PHP).

    What exactly is the effect you are looking for? Do you wish to simply display the current date/time, in the user's timezone, on the page? If so, you can just do it purely client-side:
    Code:
    if (window.addEventListener)
      window.addEventListener('load', doOnload, false);
    else if (window.attachEvent)
      window.attachEvent('onload', doOnload);
    
    var timeDisplay;
    
    function doOnload()
    {
      timeDisplay = document.getElementById('time-display');
      setInterval(updateTime, 1000);
    }
    
    function updateTime()
    {
      var currentTime = new Date()
      var hours = currentTime.getHours()
      var minutes = currentTime.getMinutes()
      var timeStr;
    
      if (minutes < 10)
        minutes = '0' + minutes
    
      timeStr = hours + ':' + minutes + ((hours > 11) ? 'pm' : 'am');
    
      if (timeDisplay.hasChildNodes())
        timeDisplay.firstChild.data = timeStr;
      else
        timeDisplay.appendChild(document.createTextNode(timeStr));
    }
    and in your HTML, wherever you want:
    HTML Code:
    <span id="time-display"></span>
    If you would like me to explain any of the Javascript please say and I will do so.
    Last edited by penagate; Jun 4th, 2006 at 09:47 PM. Reason: Forgot one function :-P

Posting Permissions

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



Click Here to Expand Forum to Full Width