Page 2 of 2 FirstFirst 12
Results 11 to 19 of 19

Thread: PHP TIME() and birthday

  1. #11
    Join Date
    Mar 2011
    Posts
    2,145
    Thanks
    59
    Thanked 116 Times in 113 Posts
    Blog Entries
    4

    Smile

    Yes!!! This is much easier, thanks JShor. Could you please do one more thing. If the select the month august, I would like the dropdown menu of days to be up to 31. However if they selected March then it would only go up to 30, because March only has 30 days in it etc...

    Any help would be great!

  2. #12
    Join Date
    Mar 2007
    Location
    New York, NY
    Posts
    557
    Thanks
    8
    Thanked 66 Times in 66 Posts

    Default

    Hmm... Interesting problem. Well, this should work for you:

    Code:
    <script type="text/javascript">
    var strHTML = "";
    
    function chgDayCount(obj) {
        if(obj.value == 'September' || obj.value ==  'April' || obj.value ==  'June' || obj.value ==  'November') {
            for(i=1; i<31; i++) {
                strHTML += '<option value="' + i + '">' + i + '</option>';
            }
            
            document.getElementById('days').innerHTML = strHTML;
        } else {
            for(i=1; i<=31; i++) {
                strHTML += '<option value="' + i + '">' + i + '</option>';
            }
            
            document.getElementById('days').innerHTML = strHTML;
        }
    }
    </script>
    Set the onchange event in the <select> menu with the name of month to chgDayCount(this) set the ID of the <select> named "day" to "days", and the ID of the <select> named "month" to "months". The code is pretty self-explanatory, I don't think I need to document it.

    I think March has 31 days.
    30 days has September, April, June and November.
    Last edited by JShor; 09-04-2011 at 11:49 AM.
    - Josh

  3. #13
    Join Date
    Mar 2007
    Location
    New York, NY
    Posts
    557
    Thanks
    8
    Thanked 66 Times in 66 Posts

    Default

    Oh, I didn't address the problem that it is likely (as it is with Firefox, Chrome, etc) that menu values are saved when the page is refreshed. So there poses the circumstance where the user will enter "November" and then refresh, and the option to select 31 as the day is possible.

    You can solve this with a document ready check and just call the function with the object argument called by getElementById() and it will refresh the menu to have only 30 days (if in the case that September, April, June or November is selected).

    If you're using jQuery, this is very easy to code:
    Code:
    <script type="text/javascript">
    $(document).ready(function() {
    chgDayCount(document.getElementById('months'));
    });
    </script>
    If you're not... well... then you have a lot of coding to do.
    - Josh

  4. #14
    Join Date
    Mar 2011
    Posts
    2,145
    Thanks
    59
    Thanked 116 Times in 113 Posts
    Blog Entries
    4

    Default

    Thanks for all your help JShor

    PHP Code:
    <?php

    $month 
    11;
    $day 17;
    $year 1992;

    $date "$month/$day/$year";
    $time strtotime($date);

    $age floor($time / (60*60*24*365));

    echo 
    "You are $age years old!";

    ?> 
    <?php

    $month 
    = array('January''February''March''April''May''June''July''August''September''October''November''December');

    echo 
    " <select name=\"month\" onchange=\"chgDayCount(this)\" id=\"months\">";
    foreach(
    $month as $m) {
        echo 
    "<option value=\"$m\">$m</option>";
    }
    echo 
    "</select>";

    $d 0;

    echo 
    " <select name=\"day\" id=\"days\">";
        while(
    $d 31) {
            
    $d++;
            echo 
    "<option value=\"$d\">$d</option>";
        }
    echo 
    "</select>";

    $y date("Y");

    echo 
    " <select name=\"year\">";
        while(
    $y >= date("Y"strtotime("-100 year"))) {
            echo 
    "<option value=\"$y\">$y</option>";
            
    $y--;
        }
    echo 
    "</select>";


    ?>

    <html>
    <head>
    <script type="text/javascript">
    var strHTML = "";

    function chgDayCount(obj) {
        if(obj.value == 'September' || obj.value ==  'April' || obj.value ==  'June' || obj.value ==  'November') {
            for(i=1; i<31; i++) {
                strHTML += '<option value="' + i + '">' + i + '</option>';
            }
            
            document.getElementById('days').innerHTML = strHTML;
        } else {
            for(i=1; i<=31; i++) {
                strHTML += '<option value="' + i + '">' + i + '</option>';
            }
            
            document.getElementById('days').innerHTML = strHTML;
        }
    }
    </script>
    </head>
    <body>
    </body>
    </html>
    Is that what it should look like because when I select a month the days field goes blank???

    Quote Originally Posted by JShor View Post
    I think March has 31 days.
    30 days has September, April, June and November.
    AWKWARD

    Also I'm not using JQuery I once read about it but i have no clue about it. Could you give me a link to a page about it.

  5. #15
    Join Date
    Mar 2007
    Location
    New York, NY
    Posts
    557
    Thanks
    8
    Thanked 66 Times in 66 Posts

    Default

    It goes blank? What browser/version are you using? It works fine for me in FF 3.6+, IE, Opera and Chrome.

    jQuery is easy to include. You just need to download the JS and include it in your code, and you can the programming interface to make writing JavaScript much faster.
    http://jquery.org/
    - Josh

  6. #16
    Join Date
    Mar 2011
    Posts
    2,145
    Thanks
    59
    Thanked 116 Times in 113 Posts
    Blog Entries
    4

    Default

    I'm using intetnet explorer 8

  7. #17
    Join Date
    Mar 2011
    Posts
    2,145
    Thanks
    59
    Thanked 116 Times in 113 Posts
    Blog Entries
    4

    Default

    I just downloaded jQuery. How do I use it for what I'm trying to do?

  8. #18
    Join Date
    Mar 2007
    Location
    New York, NY
    Posts
    557
    Thanks
    8
    Thanked 66 Times in 66 Posts

    Default

    You're right, I see it in IE8 but not IE9. Looks like IE8- is the only browser with that problem. I'll look into it and let you know what I come up with.
    - Josh

  9. #19
    Join Date
    Mar 2011
    Posts
    2,145
    Thanks
    59
    Thanked 116 Times in 113 Posts
    Blog Entries
    4

    Default

    Thanks JShor ,so much, for all your help!

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
  •