SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Zealot dragonfly7's Avatar
    Join Date
    May 2003
    Location
    ontario
    Posts
    156
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Concatenating dates from pull-downs

    We have a form with three pull-down menus (yr,month,day). The form seems to submit ok but the dates are incorrect. We want the year to start at the current year-1 and for the current date to be selected on default.

    Here is the code for the form:
    PHP Code:
    //create an array of months
    $monthname = array(1=> "January""February""March""April""May""June""July""August""September""October""November""December");
    //store today's date
    $today Time();
    //formats today's date
    $f_today date("M-d-Y",$today);
    $todaymo date("m",$today);
    echo 
    "
       <table align=left border=0 cellpadding=4> 
          <tr> 
          <td valign=top> 
            <div align=left><p>
    $articledateLabel</p></div></td>
          <td valign=top>
            <select name='datemo'>\n"
    ;
            for (
    $n=1;$n<=12;$n++)
            {
            echo 
    " <option value=$n\n";
            if (
    $todaymo == $n)
            {
            echo 
    " selected";
            }
            echo 
    "> $monthname[$n]\n";
            }
            echo 
    "</select>";
            
    $todayday date("d",$today);
            echo 
    "<select name='dateday'>\n";
            for (
    $n=1;$n<=31;$n++)
            {
            echo 
    "<option value=$n";
            if (
    $todayday == $n )
            {
            echo 
    " selected";
            }
            echo 
    "> $n\n";
            }
            echo 
    "</select>\n";
            
    $startyr = (date("Y"$today)-1);
            echo 
    "<select name='dateyr'>\n";
            for (
    $n=$startyr;$n<=$startyr+1;$n++)
            {
            echo 
    "<option value=$n";
            if (
    $startyr+== $n )
            {
            echo 
    " selected";
            }
            echo 
    "> $n\n";
            }
            echo 
    "</select></td></tr>\n"
    Here is the processing file:

    PHP Code:
    $articledate $dateyr.$datemo.$dateday;
    $sql "INSERT INTO tablename (articledate, title, content, category) VALUES ('$articledate', '$title', '$content', '$category')"
    Any suggestions?
    Thanks!

  2. #2
    SitePoint Zealot dragonfly7's Avatar
    Join Date
    May 2003
    Location
    ontario
    Posts
    156
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Oops, forgot to mention that the year comes up as 2020 and the month and dates are also incorrect.
    When we choose 2004-01-18 the processing file displays it as 2004118 and the database displays it as 2020-04-11.
    Weird!

  3. #3
    Sell crazy someplace else markl999's Avatar
    Join Date
    Aug 2003
    Location
    Manchester, UK
    Posts
    4,007
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Does this get you any closer?
    PHP Code:
    <?php
    //create an array of months
    $monthname = array('January''February''March''April''May''June''July''August''September''October''November''December');
    $today explode(' 'date('F d Y'time()));
    ?>
    <table align=left border=0 cellpadding=4>
      <tr>
        <td valign=top>
            <div align=left><p>articledateLabel</p></div></td>
        <td valign=top>
        <select name='datemo'>
        <?php
        
    foreach($monthname as $key=>$month){
          
    $selected '';
          if(
    $month == $today[0]){
            
    $selected ' selected';
          }
          echo 
    '<option value="'.$key.'"'.$selected.'>'.$month."</option>\n";
        }
        
    ?>
        </select>
        <select name='dateday'>
        <?php
          
    for ($n=1;$n<=31;$n++)
          {
            echo 
    "<option value=$n";
            if (
    $today[1] == $n )
            {
              echo 
    ' selected';
            }
            echo 
    "> $n\n";
          }
          
    ?>
          </select>
          <select name='dateyr'>
          <?php
          
    for ($n=$today[2];$n<=$today[2]+1;$n++)
            {
            echo 
    "<option value=$n";
            if (
    $today[2]+== $n )
            {
              echo 
    ' selected';
            }
            echo 
    "> $n\n";
          }
          
    ?>
          </select>
        </td>
      </tr>
    </table>

    //processing file
    $articledate = $_POST['dateyr'].'-'.$_POST['datemo'].'-'.$_POST['dateday'];
    $sql = "INSERT INTO tablename (articledate, title, content, category) VALUES ('$articledate', '$title', '$content', '$category')";

  4. #4
    SitePoint Zealot dragonfly7's Avatar
    Join Date
    May 2003
    Location
    ontario
    Posts
    156
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Almost!!!

    Two little things are not quite right -

    1. The years in the pull down menu are displaying as 2004 and 2005, can we change this to display 2003 and 2004 instead? (we want the current year displayed by default but also have the option to choose the previous year)

    2. When the record is submitted the year and day are correct but the month is out by 1. For example March 19, 2004 (2004-03-19) is being inserted into the database as Feb. 19, 2004 (2004-02-19)

    Thanks for your help! These forums are amazing.

  5. #5
    SitePoint Zealot dragonfly7's Avatar
    Join Date
    May 2003
    Location
    ontario
    Posts
    156
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just noticed another problem

    It only works if a double digit day is selected ie. 10-31). If a single digit day (1-9) is selected the record is inserted into the database with the year 2020 and the wrong day and month.

  6. #6
    Sell crazy someplace else markl999's Avatar
    Join Date
    Aug 2003
    Location
    Manchester, UK
    Posts
    4,007
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    <select name='dateday'>
        <?php
          
    for ($n=1;$n<=31;$n++)
          {
            echo 
    '<option value="'.($n+1).'"';
            if (
    $today[1] == $n )
            {
              echo 
    ' selected';
            }
            echo 
    "> $n\n";
          }
          
    ?>
          </select>
          <select name='dateyr'>
          <?php
          
    for ($n=$today[2];$n>=$today[2]-1;$n--)
            {
            echo 
    "<option value=$n";
            if (
    $today[2] == $n )
            {
              echo 
    ' selected';
            }
            echo 
    "> $n\n";
          }
          
    ?>
          </select>
    Try that then instead

  7. #7
    Sell crazy someplace else markl999's Avatar
    Join Date
    Aug 2003
    Location
    Manchester, UK
    Posts
    4,007
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    As for the single digit day thing, try :
    $articledate = sprintf('%2d-%02d-%02d', $_POST['dateyr'],$_POST['datemo'],$_POST['dateday']);

  8. #8
    SitePoint Zealot dragonfly7's Avatar
    Join Date
    May 2003
    Location
    ontario
    Posts
    156
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Almost there (I hope!!)

    The year is displaying properly and defaulting to the current date which is fantastic!

    but....

    the submitted dates are still not being added properly to the database. Still getting the year 2020 and wrong month and day except now it is happening with single and double digit days?!?


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
  •