I have an editable form, that (upon other things) retrieves a date from mysql. To prepopulate this text field, I use:

 $result = mysql_query("SELECT DATE_FORMAT(Expiration_Date, '%d-%b-%y') AS DateX FROM MyTable) or exit(mysql_error());

Which will return (for example): 01-Jan-06

I have a dropdown field that a user can use to select the number of days to add to this date. In addition, however, the user should also be allowed to opt to not use the dropdown, but type in the value instead, in the d-M-y format.

So, to continue the code...

        while ($row = mysql_fetch_assoc($result)) {
        $expdate = $row['DateX'];


            echo "<td><input type=\"text\" name=\"expdate\" value=\"$expdate\"></td>
                  <td valign=\"top\"><b>Add days:</b> 
                    <select name=\"adddays\" class=\"selects\">";
                            for ($day=0; $day<=31; $day++) {
                                    echo "<option value=\"$day\">$day</option>\n";
                            }
            echo "</select>";

So, I want to add the expdate and the adddays fields and enter that into mysql, formatting as yyyy-mm-dd hh-mm-ss.

This is what I've tried, but it's not working:

        if (!empty($_POST['expdate'])) {
                $xdate1 = date("Y-m-d H:i:s", $_POST['expdate']);
                $xdate2 = strtotime("+".$adddays." days", $xdate1); 
        } else { 
                $arrErrors['expdate'] = 'Please set the expiration date.';
        } 

All this does is return (for example) 88406. Can someone help?

    Firstly what is the supposed format of expdate? If its in the format like dd-mm-YYYY or similar formats it wont work. It must be a timestamp, like using the [man]time()[/man] function.

    If you wanted to force it to change to a specific format you may want to look into using mktime first before trying to get the date function to actually do it. This will firstly create it into a timestamp and than you can use mktime again to add how many days for the second field.

      The expected format is dd-mmm-yy (01-Jan-06), thus the line:

      $xdate1 = date("Y-m-d H:i:s", strtotime($_POST['expdate'])); 
      

      Which then formats the entered date as 2006-01-10 00:00:00. Basically, I want to know how to add x days to $xdate1.

      I thought:

       $xdate2 = strtotime("+7 days", $xdate1);

      would do exactly this. It's not. I need to know how.

        Nevermind, this worked:

                if (!empty($_POST['expdate'])) {
                        $xdate1 = date("Y-m-d H:i:s",strtotime($_POST['expdate']));
                        $xdate2 = strtotime("$xdate1 + $validity days");
                        $xdate3 = date("Y-m-d H:i:s", $xdate2); 
                } else { 
                        $arrErrors['expdate'] = 'Please set the expiration date.';
                } 
        

          It's not clear, but is there a reason you're formatting the date as "dd-MMM-yy" in the first place instead of "yyyy-mm-dd" if that's what you're wanting?

            Write a Reply...