Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 13 of 13
  1. #1
    Regular Coder
    Join Date
    Jun 2002
    Location
    UK
    Posts
    262
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default values to todays date

    I have 3 select (combo) boxes...


    One for day (for loop 1-31)
    One for Month - Values Jan, Feb, Mar etc (3 digit)
    One for year..


    How could I default the values to be set to the current date? I've done this in ASP but never in PHP - any ideas?

  • #2
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Sure. All you need is Date()
    http://be.php.net/date

    Then , when inside you'r loops to build the optionlists, you have something like
    PHP Code:
    for ($i=1$i<=31;$i ++){
       
    $option .=  "<option value=\"" $i "\"" ;
       if 
    Date(d) == $i {
           
    $option .= " selected=\"selected\"";
       }
       
    $option .= ">" $i "</option>" ;

    echo 
    $option 
    So the option with the same dayvalue will be selected.
    For the month, it's Date(m), and for the year, Date(Y)

  • #3
    Regular Coder
    Join Date
    Jun 2002
    Location
    UK
    Posts
    262
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Raf,

    Thanks for your reply...

    Currently I have the select field displaying the info selected (the form and results are on one page)

    So Initally before a user has selected the date I would like it to be the current date. If the user has already selected the date and viewed the results, I would like the selected date remain there (if you get me!)

    Here is my code...

    <?php
    for($i = 1; $i < 32; $i++){
    echo '<option value="' .$i. '"'
    .((isset($_GET['Day']) && $_GET['Day'] == $i) ? ' selected="selected"' : '').
    '>' .$i. '</option>';
    }

    How could I get it to initally put the default date (day in this example)

    Thanks

  • #4
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Ha yes. Why not first get the "selected" date and store it in a variabel, and then have the loop where whe check agains it. Like

    PHP Code:
    if (isset ($_GET['Day'])) {  //so when the first first loads, this will be False
      
    $select $_GET['Day'] ;
    }
    else {
      
    $select Date(d) ;
    }  
    for (
    $i=1$i<=31;$i ++){
       
    $option .=  "<option value=\"" $i "\"" ;
       if 
    $select == $i {
           
    $option .= " selected=\"selected\"";
       }
       
    $option .= ">" $i "</option>" ;

    echo 
    $option 

  • #5
    Regular Coder
    Join Date
    Jun 2002
    Location
    UK
    Posts
    262
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Raf,

    Thanks thats great... works like a treat too.

    My next prob is with the month. Currently it is like (for each month):

    <option value="Jan" <?php if($_GET['Month'] == "Jan"){ echo 'selected="selected"';} ?> />Jan</option>

    How could I get it to default to the current month if a month has not been selected? Would we need an else clause in each of the statements?

    Thanks for your help

  • #6
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    PHP Code:
    if (isset ($_GET['Month'])) {  //so when the first first loads, this will be False
      
    $select $_GET['Month'] ;
    }
    else {
      
    $select Date(M) ; // will return the current month as a 3 letter abreviation

    Then, when you build the optionlist, you check if the option == $select . Like

    <option value="Jan" <?php if($select == "Jan"){ echo 'selected="selected"';} ?> />Jan</option>

  • #7
    Senior Coder
    Join Date
    Jun 2002
    Location
    frankfurt, german banana republic
    Posts
    1,848
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I'm just going to add a minor correction to your code, raf. The following is slightly incorrect usage of the date() function:

    PHP Code:
    $select Date(M) ; 
    M is not a defined constant. What happens is that PHP *assumes* you rather meant M in a literal way, as if it would be a string, and treats it like that. This is general behaviour and not limited to the date function. However, depending on the error level you set, this triggers an error notice.
    The format string really needs to be a string, like

    PHP Code:
    $select Date('M'); 
    This allows you to combine various date identifier to form a date string:

    PHP Code:
    $select date('d.m.Y'); // example 
    Nitpicking as usually.
    De gustibus non est disputandum.

  • #8
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Originally posted by mordred
    Nitpicking as usually.
    It's a valid remark ... and now I also learned something today

  • #9
    Regular Coder
    Join Date
    Jun 2002
    Location
    UK
    Posts
    262
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thats great! Got that working a treat!

    Theres another twist to the tale though now! It been decided that there will be 2 sets of dates - a date from and a date to. The date to is the current date (which we've done) and the date from is the current date -7 (a week previous).

    The day from will simply be:

    if (isset ($_GET['DayFrom'])) { //so when it first loads, this will be False
    $select = $_GET['DayFrom'];
    }
    else {
    $select = Date('d')-7 ;

    etc....

    How could we do the month from if say the current date is the 3rd of October - how can we make the month show September?

    Cheers for all your help - I'm sure this is the last request on dates!!

  • #10
    Senior Coder
    Join Date
    Jun 2002
    Location
    frankfurt, german banana republic
    Posts
    1,848
    Thanks
    0
    Thanked 0 Times in 0 Posts
    There's a function called strtotime() which you can utilize with natural language. For your case, to display the name of the month one week ago, simply do this:

    PHP Code:
    $month date('F'strtotime('1 week ago'));
    print 
    $month
    De gustibus non est disputandum.

  • #11
    Regular Coder
    Join Date
    Jun 2002
    Location
    UK
    Posts
    262
    Thanks
    0
    Thanked 0 Times in 0 Posts
    mordred - I can see how that works - thanks!

    but when i try:

    <?php
    if (isset ($_GET['Month'])) { //so when the first first loads, this will be False
    $select = $_GET['Month'] ;
    }
    else {
    $select = Date('F', strtotime('1 week ago')); // will return the current month as a 3 letter abreviation
    }
    ?>
    <option value="Jan" <?php if($select == "Jan"){ echo 'selected="selected"';} ?> />Jan</option>
    .....


    It returns Jan - is this because the strtotime doesn't return it back as a 3 letter abbreviation? Any clues how to get the value back as Jan, Feb etc?

  • #12
    Senior Coder
    Join Date
    Jun 2002
    Location
    frankfurt, german banana republic
    Posts
    1,848
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Yes indeed, the modifier is not the correct one, I thought you wanted the whole month name - in this case you'd need date('F'), but what you're looking for seems to be date('M').

    You can always look up the right modifier for your needs in this table:
    http://us2.php.net/manual/en/function.date.php
    De gustibus non est disputandum.

  • #13
    Regular Coder
    Join Date
    Jun 2002
    Location
    UK
    Posts
    262
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Cheers - silly me!!

    I can't test it thou because I can't change the date on the server but I trust it works!!

    Thanks every1 for you help!


  •  

    Posting Permissions

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