0

I want to add 'x' number of days to current date. The days will be select by user and add to current date. Below is my select days code

<html>
<head>
    <meta charset="UTF-8">
    <title>Date</title>
</head>
<body>
        <form name=date method="post" action="test.php">
            <table style="width: 60%;">
                <tr>
                    <td colspan="2" align="center"><h3>Date</h3></td>
                </tr>
                <tr>
                    <td>&nbsp;</td>
                    <td>&nbsp;</td>
                </tr>
                <tr>
                    <td>No. of Days</td>
                    <td>
                        <select name="days" style="width: 270px">
                            <option value ="Select">Select</option>
                            <option value="7">7</option>
                            <option value ="14">14</option>
                            <option value ="21">21</option>
                            <option value ="28">28</option>
                        </select>
                    </td>
                </tr>
                <tr align="center">
                    <td><input type='submit' value='Submit'></td>
                    <td><input type='reset' value='Reset'></td>
                </tr>

            </table>
        </form>
</body>

Below is the code after submit

<?php
$date = date("Y-m-d");
$days = $_POST["days"];
echo date('Y-m-d', strtotime($date . ' + '. $_POST["days"]));
?>

But the results will always show as "1970-01-01"

Hope to get some tips. Thanks in advance

Justin Chong
  • 3
  • 1
  • 4
  • Possible duplicate of [Adding days to $Date in PHP](https://stackoverflow.com/questions/3727615/adding-days-to-date-in-php) – Scuzzy Jan 27 '18 at 04:10
  • You might get away changing the `value` in your `option`s to ready for example `"7 days"` instead of just `"7"`. – sal Jan 27 '18 at 04:13

2 Answers2

4

There something wrong with your code within strtotime() function try any of these to get the same result as you want...

By strtotime():

echo date('Y-m-d', strtotime($Date. ' + '.$_POST["days"].' days')); 
// This can be also written as strtotime('+'.$_POST['days'].' days '.$Date);

By modify():

$date = new DateTime($Date);
$date->modify('+'.$_POST["days"].' day');
A.D.
  • 2,352
  • 2
  • 15
  • 25
0

Please try this

$date=date_create("2013-03-15");
date_add($date,date_interval_create_from_date_string("40 days"));
echo date_format($date,"Y-m-d");
Priya
  • 1,410
  • 14
  • 22