#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2011
    Posts
    1
    Rep Power
    0

    Smile Quote that changes weekly


    I was told javascript may be an option that would work but wanted to hear your advice.

    I want to show a quote on a website for one week and then change to another quote a week later, say every Monday it changes? I have 52 quotes for the year. Im not sure how to write it up? I also wanted the first letter of the Quote to be a large letter to make it look funky.

    Someone helped me with this below but this way just changes random when the page is refreshes. not really what I want.

    Can you please help me and show me where about top paste it in Html. thanks.

    <script type="text/javascript">
    verses =
    [
    "quote 1",
    "quote 2",
    "quote 3",
    "quote 4",
    "quote 5",
    ]
    var keyword = verses[Math.floor(Math.random()*verses.length)]

    document.write(keyword);
    </script>

    thanks
  2. #2
  3. No Profile Picture
    Lost in code
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 2004
    Posts
    8,301
    Rep Power
    7170
    It randomly changes because your index into the verses array is based on Math.random(); if you want to display a new quote for each week of the year you need to base it on the current date instead.

    JavaScript has a Date object that you can use to fetch information about the current date. Unfortunately, the week of the year isn't part of that information, although you could program code to calculate it if you want.

    The modulo operator (%) can be used to restrict a value to a certain range.

    Code:
    var now = (new Date()).getTime();
    var index = Math.round(now/3600000.0) % verses.length;
    var keyword = verses[index];
    For example, this code will take the current timestamp in milliseconds [now], divide it by 3600000.0 milliseconds (one hour), then divide it by the number of quotes you have [verses.length] and take the remainder (using the modulo operator).

    Effectively, this produces a number (index) between 0 and 4 that increases to the next number once per hour and wraps back around to 0 after it exceeds 5. This number can be used as an index into your verses array.

    For an approximate solution you could change 3600000.0 so that it represents 7 days in milliseconds instead of 1 hour in milliseconds. Then you would have code that cycles through a list of quotes, moving to the next one approximately once a week.

    A more precise solution is possible of course, but requires more effort because you will need to compute the current week of the year.
    PHP FAQ

    Originally Posted by Spad
    Ah USB, the only rectangular connector where you have to make 3 attempts before you get it the right way around

IMN logo majestic logo threadwatch logo seochat tools logo