SitePoint Sponsor

User Tag List

Results 1 to 12 of 12
  1. #1
    SitePoint Wizard
    Join Date
    Jul 2006
    Location
    New Zealand
    Posts
    1,300
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Javascript Date Script

    How can i display a java script date what i mean is

    between hours 1am to 12midday good morning
    1pm to 5pm good afternoon
    6pm to 9pm good eveening
    10 to 12 good nite

    How can i do that using Javascript and if javascript is not on it will display the php version instead.

    So how can this be done?

  2. #2
    SitePoint Wizard
    Join Date
    Jul 2003
    Location
    Kent
    Posts
    1,921
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Code:
     
    var dayNames = new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");
    
      var monthNames = new Array("January","February","March","April","May","June","July",
                               "August","September","October","November","December");
    
      var today = new Date();
      var yr  = today.getYear();
      var hrs = today.getHours();
      var greeting = "";
      
      if (hrs <=11 && hrs > 4 )
        greeting = "Good morning, on ";
      else if (hrs > 11 && hrs <17)
        greeting = "Good afternoon, on ";
      else
        greeting = "Good evening, on ";
          
    
      if (yr < 1000) 
        yr +=1900;
    
      document.write(greeting + dayNames[today.getDay()] + ", " + monthNames[today.getMonth()] + " " + today.getDate() + ", " + yr);
    save as date.js in your scripts folder

    Insert this where you want the date, etc

    <script type="text/JavaScript" language="Javascript" src="scripts/dated.js"> </script>

    Alter the greeting as it suits you.

  3. #3
    SitePoint Wizard
    Join Date
    Jul 2006
    Location
    New Zealand
    Posts
    1,300
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Can i include the persons session name in the greetings format so it says

    good mornin admin, etc..?

    so for that do i put

    greeting="Good Morning,"; <?php
    echo $_SESSION['user'];

    ?>

    am i correct?

    also can i include the time of day using the javascript too?

    how will i go about doing that?

  4. #4
    SitePoint Wizard
    Join Date
    Jul 2006
    Location
    New Zealand
    Posts
    1,300
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This is what i want it to look like.

    PHP Code:
                    echo "<script type=\"text/JavaScript\" language=\"Javascript\">
                    var dayNames = new Array(\"Sunday\",\"Monday\",\"Tuesday\",\"Wednesday\",\"Thursday\",\"Friday\",\"Saturday\");

      var monthNames = new Array(\"January\",\"February\",\"March\",\"April\",\"May\",\"June\",\"July\",
                               \"August\",\"September\",\"October\",\"November\",\"December\");

      var today = new Date();
      var yr  = today.getYear();
      var hrs = today.getHours();
      var greeting = \"\";
      
      if (hrs <=11 && hrs > 4 )
        greeting = \"Good morning on"
    ;
         echo 
    $_SESSION['user']; echo "\";
      else if (hrs > 11 && hrs <17)
        greeting = \"Good afternoon,"
    ;
        echo 
    $_SESSION['user']; echo "\";
      else
        greeting = \"Good evening"
    ;
        echo 
    $_SESSION['user']; "\";
      if (yr < 1000) 
        yr +=1900;

      document.write(greeting + dayNames[today.getDay()] + \", \" + monthNames[today.getMonth()] + " " + today.getDate() + \", \" + yr);
                    </script>"

    but gives me this error


    Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING in C:\Program Files\xampp\htdocs\BillMayersArtGalleryCms\Public_html\admin\index.php on line 36

    Why?

  5. #5
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,862
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Don't use getYear() as it is not Y2K compliant. If you need to retrieve the year use getFullYear() instead.

    If you want to enclose the Javascript into a PHP echo command then it all has to go on one line. You use \n where you want the PHP to output the line breaks.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  6. #6
    SitePoint Wizard
    Join Date
    Jul 2006
    Location
    New Zealand
    Posts
    1,300
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    will that fix my problem?

  7. #7
    SitePoint Wizard
    Join Date
    Jul 2003
    Location
    Kent
    Posts
    1,921
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Your site looks very different in FF, ie6 and ie7, with different bits on display in each browser! Only IE6 seems to show what I think you want. It's completely unusable in IE7 and FF.

    PS I just broke into your testimonial client's site's admin section at the first (!!!!) attempt, browsing with ie6 and ie7! FF doesn't seem to get me in though, probably some setting I have in FF. And I know nothing about how to hack a site!

  8. #8
    SitePoint Wizard
    Join Date
    Jul 2006
    Location
    New Zealand
    Posts
    1,300
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I Know about firefox but this is not for my portfolio but another projects its for a custom content management system.

    Thanks again for the help.

    Can anyone tell me what i am doing wrong?

  9. #9
    SitePoint Wizard
    Join Date
    Jul 2006
    Location
    New Zealand
    Posts
    1,300
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Can anyone help me with this please?

    How i can display the user who is logged in using javascript with the php function $_session['user'];

    ?

  10. #10
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,862
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    You would have to generate the Javascript using PHP eg.

    var user = '<?php echo $_session['user']; ?>';

    and then reference the Javascript field that you have loaded the PHP field into.

    This code would need to be in a file with a PHP extension so if you are using an external Javascript use:

    <script type="text/javascript" src="myjs.php"></script>
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  11. #11
    SitePoint Wizard
    Join Date
    Jul 2006
    Location
    New Zealand
    Posts
    1,300
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So i put that javascript code above in an javascript file and then go var echoing the user of the person logged in?

    Would i need to make any changes to javascript code?

  12. #12
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,862
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    As long as that line of code is in a file with a .php extension it will get processed as PHP on the server. So if the person's user name was "Fred" the PHP would convert it to read

    var user = 'Fred';

    and you wouldjust reference it from within the Javascript normally after that.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">


Bookmarks

Posting Permissions

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