SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Wizard WorldNews's Avatar
    Join Date
    Nov 2007
    Posts
    1,027
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    How do I set the date in a <select> based field to be todays?

    Hello,

    How do I set the date in a <select> based field to be todays?
    To be exact I have a date range selection for issuing a MySQL query like:

    Start date: [day] [month] [year]

    and I want the values for day, month, year to be todays date.
    How does one do that in PHP?
    Of course [day] [month] [year] are all <select> based drop down lists
    and their values are generated by PHP code using for loops.
    So my question is how do I find out what todays date is and then set
    [day] to todays value, [month] to be current month value, etc?

    Regards,

    Anoox search engine volunteer

    www.anoox.com

  2. #2
    SitePoint Wizard silver trophybronze trophy Stormrider's Avatar
    Join Date
    Sep 2006
    Location
    Nottingham, UK
    Posts
    3,133
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Look at the php date() function:

    http://uk2.php.net/manual/en/function.date.php

    This way you can easily find out the current day, month and year. Then in your loops, output 'selected="selected"' if they match the current date.

  3. #3
    SitePoint Evangelist
    Join Date
    May 2006
    Location
    Denmark
    Posts
    407
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Something like this?

    Code PHP:
    echo '<select name="day">';
    for ($i = 1; $i <= 31; $i++) {
    	$selected = date('j') == $i ? ' selected="selected"' : null;
    	echo '<option' . $selected . '>' . $i . '</option>';
    }
    echo '</select>';

    Same concept for month and year of course.

  4. #4
    SitePoint Wizard WorldNews's Avatar
    Join Date
    Nov 2007
    Posts
    1,027
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    ThanX for this tip.
    It was just the tip that I needed to quickly get it done.

    Have a great day all

    Quote Originally Posted by Stormrider View Post
    Look at the php date() function:

    http://uk2.php.net/manual/en/function.date.php

    This way you can easily find out the current day, month and year. Then in your loops, output 'selected="selected"' if they match the current date.

    Anoox search engine volunteer

    www.anoox.com


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
  •