SitePoint Sponsor

User Tag List

Results 1 to 18 of 18
  1. #1
    SitePoint Wizard bronze trophy PicnicTutorials's Avatar
    Join Date
    Dec 2007
    Location
    Carlsbad, California, United States
    Posts
    3,656
    Mentioned
    15 Post(s)
    Tagged
    0 Thread(s)

    Looking for automatic age updater?

    Looking for automatic age updater? I googled for a bit but couldnt find it. I always forget to update my age on my site so Im looking for a way to make this automatic like a auto current year one I have for my copyright. Thanks!

    So my sentence would read... Hello name name is yada - yada my age is 35 (js goes here in place of 35).

  2. #2
    SitePoint Wizard donboe's Avatar
    Join Date
    Jun 2010
    Location
    Netherlands
    Posts
    2,063
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by EricWatson View Post
    Looking for automatic age updater? I googled for a bit but couldnt find it. I always forget to update my age on my site so Im looking for a way to make this automatic like a auto current year one I have for my copyright. Thanks!

    So my sentence would read... Hello name name is yada - yada my age is 35 (js goes here in place of 35).
    Hi Eric can't you just use nearly the same script you're using for the copyright. Just set the d.o.b. and use a datediff between now() and d.o.b.

    I'm not that familiar with P.H.P but here is how I would do it in Coldfusion
    Code CFM:
    <cfset dob = dateFormat( '1961-03-18', 'yyyy-mm-dd' )/>
    <cfset nowDate = dateFormat( now(), 'yyyy-mm-dd' ) />
     
    <cfset age = dateDiff( 'yyyy', dob, nowDate  ) />
    <cfdump var="#age#" />

  3. #3
    SitePoint Wizard bronze trophy PicnicTutorials's Avatar
    Join Date
    Dec 2007
    Location
    Carlsbad, California, United States
    Posts
    3,656
    Mentioned
    15 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by donboe View Post
    Hi Eric can't you just use nearly the same script you're using for the copyright. Just set the d.o.b. and use a datediff between now() and d.o.b.
    Wow! While sitting in my truck I have my music streaming from my iPhone playing through my truck speakers, and all awhile multitasking and reading/replying to this post, and launch dropbox to see and copy my copyright code into this post - tecknowledgey is amazing! I am now 100% on my phone. Aside from website edits I have completely abandoned my desktop. I fax, email, scan, files, banking, etc, etc all from my phone.

    OK, here is my copyright code. I don't think I can do that. Or can I?

    <script>document.write("2002 - "+ new Date().getFullYear());</script>

  4. #4
    SitePoint Wizard donboe's Avatar
    Join Date
    Jun 2010
    Location
    Netherlands
    Posts
    2,063
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Eric here is A Link to date_diff using javascrip.

    Enjoy your music

  5. #5
    SitePoint Wizard bronze trophy PicnicTutorials's Avatar
    Join Date
    Dec 2007
    Location
    Carlsbad, California, United States
    Posts
    3,656
    Mentioned
    15 Post(s)
    Tagged
    0 Thread(s)
    Thanks but unless mistaken I don't believe that will work. I don't need a date updated I need a age (eg 35) updated on a specific date each year.

  6. #6
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,683
    Mentioned
    99 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by EricWatson View Post
    Thanks but unless mistaken I don't believe that will work. I don't need a date updated I need a age (eg 35) updated on a specific date each year.
    The date documentation page should give you all the tools that you need.

    For example:

    Code javascript:
    var now = new Date();
    var birth = new Date('1961-03-18');
    var difference = now - birth;
    // Dates are set from 1970, so remove that to get the year 
    var age = new Date(difference).getFullYear() - 1970
    // age is 49
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  7. #7
    SitePoint Wizard donboe's Avatar
    Join Date
    Jun 2010
    Location
    Netherlands
    Posts
    2,063
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It should work though. If you set your birth day and you use the date_diff (YEARS) function to determine the difference between today and the your birth day it should return the number of years.

  8. #8
    SitePoint Wizard bronze trophy PicnicTutorials's Avatar
    Join Date
    Dec 2007
    Location
    Carlsbad, California, United States
    Posts
    3,656
    Mentioned
    15 Post(s)
    Tagged
    0 Thread(s)
    Cool. Ok, I'm going to show my ignorance here. So the var stuff goes in the head (or above the age/html) right? And then I call it in the HTML how? Thanks again!

  9. #9
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,683
    Mentioned
    99 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by EricWatson View Post
    Cool. Ok, I'm going to show my ignorance here. So the var stuff goes in the head right?And then I call it in the HTML how? Thanks again!
    No, because from the head you cannot interact with the body of the page.

    You place the script at the end of the body, just before the </body> tag.

    Code html4strict:
    yada yada my age is <span id="age">unknown</span>
    ...
    <script type="text/javascript">
    ...
    </script>
    </body>
    </html>

    Code javascript:
    var birth = new Date('1961-03-18');
    var now = new Date();
    var difference = now - birth;
    // Dates are set from 1970, so remove that to get the year 
    var age = new Date(difference).getFullYear() - 1970
     
    var span = document.getElementById('age');
    span.innerHTML = age;
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  10. #10
    SitePoint Wizard bronze trophy PicnicTutorials's Avatar
    Join Date
    Dec 2007
    Location
    Carlsbad, California, United States
    Posts
    3,656
    Mentioned
    15 Post(s)
    Tagged
    0 Thread(s)
    Awesome - thank you Paul and donboe! I should be able to handle it from there. Then I'll hide the physical age with js so it shows with js on or off. I'll work on that tomorrow morning. Will post back if I hit trouble.

  11. #11
    SitePoint Wizard bronze trophy PicnicTutorials's Avatar
    Join Date
    Dec 2007
    Location
    Carlsbad, California, United States
    Posts
    3,656
    Mentioned
    15 Post(s)
    Tagged
    0 Thread(s)
    Hmmmm.... It works! But I don't get the math its doing? For instance, if I put in my wifes date of birth and her year born (1975) it spits out that she is 40 years old. But she is 35 years old. So I have to use a false date in order to spit out the correct age. Am I missing something? Thanks!

    <script type="text/javascript">
    var birth = new Date('1975-06-05');
    var now = new Date();
    var difference = now - birth;
    // Dates are set from 1970, so remove that to get the year
    var age = new Date(difference).getFullYear() - 1975

    var span = document.getElementById('age');
    span.innerHTML = age;
    </script>

  12. #12
    SitePoint Wizard donboe's Avatar
    Join Date
    Jun 2010
    Location
    Netherlands
    Posts
    2,063
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Like pmw57 said

    // Dates are set from 1970, so remove that to get the year

  13. #13
    SitePoint Wizard bronze trophy PicnicTutorials's Avatar
    Join Date
    Dec 2007
    Location
    Carlsbad, California, United States
    Posts
    3,656
    Mentioned
    15 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by donboe View Post
    Like pmw57 said
    I did. Using the js I just posted above it spits out that she is 40. It should be saying she is 35. She was born in 1975. However, in order to spit out 35 I need to say she was born on 1970 in the script. This is why I say I don't get the math.

  14. #14
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,683
    Mentioned
    99 Post(s)
    Tagged
    4 Thread(s)
    The script must ALWAYS subtract 1970 to arrive at the correct age. Your wifes birth year is placed in only the one location, and that is the at the top of the script.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  15. #15
    SitePoint Wizard bronze trophy PicnicTutorials's Avatar
    Join Date
    Dec 2007
    Location
    Carlsbad, California, United States
    Posts
    3,656
    Mentioned
    15 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by pmw57 View Post
    The script must ALWAYS subtract 1970 to arrive at the correct age. Your wifes birth year is placed in only the one location, and that is the at the top of the script.
    Ahhh... OK that works! Trying to learn a little here. Why 1970? Is that the magic number you just used to make the script work or does it have some deeper js meaning? And I assume the script will automatically change her birthdate to 36 on her birthday correct? Thanks!

    Oops. Just noticed! I have one more request please. I need to show both of our ages. Cant use same id twice on same page so whats the best/easiest way to do that? I prob could just use the script twice buts thats sloppy yeah? Can I coma separate the id's like... var span = document.getElementById('age,age2')? But of course our birthdates are different so maybe two scripts may be the easiest route there? Or not?

  16. #16
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,683
    Mentioned
    99 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by EricWatson View Post
    Ahhh... OK that works! Trying to learn a little here. Why 1970?
    Because 1970 is when most computers start counting dates from.
    See: http://en.wikipedia.org/wiki/Unix_time

    Quote Originally Posted by EricWatson View Post
    Oops. Just noticed! I have one more request please. I need to show both of our ages. Cant use same id twice on same page so whats the best/easiest way to do that?
    Turn the script in to a function that accepts a birth date and target location as its parameters.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  17. #17
    SitePoint Wizard bronze trophy PicnicTutorials's Avatar
    Join Date
    Dec 2007
    Location
    Carlsbad, California, United States
    Posts
    3,656
    Mentioned
    15 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by pmw57 View Post
    Because 1970 is when most computers start counting dates from.
    See: http://en.wikipedia.org/wiki/Unix_time

    Turn the script in to a function that accepts a birth date and target location as its parameters.
    Easier said than done (for me at least). I'll read up on doing that and use two scripts as my fallback. Either way I'm happy thank you. One less thing to remember to do next year.

  18. #18
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,683
    Mentioned
    99 Post(s)
    Tagged
    4 Thread(s)
    function showAge(birthDate, id) {

    }
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


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
  •