Ann Holley
Ann Holley

Reputation: 39

Convert HTML form input into date php understands

I'm looking for a way to have people visiting my website enter a date through a HTML form and then use php to subtract or add time from that date they entered in. For example, in the form they will enter their Birthday and I want php to echo 5 days after their birthday. I've looked into many ways to add dates in php, so I don't think that will be a problem. I just can't figure out how to get php to recognize form inputs as a date.

Here is my HTML code to select the date:

<select name="DMonth" input type="number">
    <option>- Select Month -</option>
    <option value=1>January</option>
    <option value=2>February</option>
    ...
    <option value=12>December</option>
</select>
<select name="DDay" input type="number">
<option>- Select Day -</option>
    <option value=1>1</option>
    <option value=2>2</option>
    ...
    <option value=31>31</option>
</select>
<select name="DYear" input type="number">
    <option>- Select Year -</option>
    <option value=2013>2013</option>
    <option value=2014>2014</option>
    ...
    <option value=2022>2022</option>
</select><br>
<select name="DHour" input type="number">
<option>- Select Hour -</option>
<option value=00>12 a.m.</option>
<option value=01>1 a.m.</option>
<option value=02>2 a.m.</option>
<option value=03>3 a.m.</option>
<option value=04>4 a.m.</option>
<option value=05>5 a.m.</option>
<option value=06>6 a.m.</option>
<option value=07>7 a.m.</option>
<option value=08>8 a.m.</option>
<option value=09>9 a.m.</option>
<option value=10>10 a.m.</option>
<option value=11>11 a.m.</option>
<option value=12>12 p.m.</option>
<option value=13>1 p.m.</option>
<option value=14>2 p.m.</option>
<option value=15>3 p.m.</option>
<option value=16>4 p.m.</option>
<option value=17>5 p.m.</option>
<option value=18>6 p.m.</option>
<option value=19>7 p.m.</option>
<option value=20>8 p.m.</option>
<option value=21>9 p.m.</option>
<option value=22>10 p.m.</option>
<option value=23>11 p.m.</option>
</select>
<select name="DMin" input type="number">
<option>- Select Minute -</option>
<option value=0>00</option>
<option value=15>15</option>
<option value=30>30</option>
<option value=45>45</option>
</select>

and this is what I thought I would use in php

$DMonth=$_POST["DMonth"];
$DDay=$_POST["DDAY"];
$DYear=$_POST["DYear"];
$DHour=$_POST["DHour"];
$DMin=$_POST["DMin"];
$Bday = date_create($DYear.'-'.$DMonth.'-'.$DDay.' '.$DHour.':'.$DMin.':00');
echo date_format($Bday, 'Y-m-d H:i:s');

but when I run the form, the only thing it echos is the current date and time.

Upvotes: 2

Views: 4252

Answers (2)

Basic
Basic

Reputation: 26766

How about...

$datetime= mktime($DHour, $DMin, 0, $DMonth, $DDay, $DYear);

?

See the mktime help page for more information - no need to build strings or worry about formats, just provide the appropriate parameters name Hour, Minute, Second, Month, Day, Year

There are also a couple of problems in your html. The syntax for a select is as follows:

<select name="DYear">
    <option>- Select Year -</option>
    <option value="2013">2013</option>

Note that the <select> has no input and that values are enclosed in quotes. There's also no need to specify type="number". To correctly poarse the values as integers instead of strings, do this in your PHP:

$DYear = (int) $_POST["DYear"];

Upvotes: 0

John Conde
John Conde

Reputation: 219824

I don't see your code for hours and minutes

This is a bit convoluted since it is an addon to your code but will do the trick.

$DMonth=$_POST["DMonth"];
$DDay=$_POST["DDAY"];
$DYear=$_POST["DYear"];
$DHour=$_POST["DHour"];
$DMin=$_POST["DMin"];
$Bday = date_create($DYear.'-'.$DMonth.'-'.$DDay.' '.$DHour.':'.$DMin.':00');
echo date_format($Bday, 'Y-m-d H:i:s');

$datetime = new DateTime(date_format($Bday, 'Y-m-d H:i:s'));
$datetime->modify('+5 days');
echo $datetime->format('Y-m-d H:i:s');

Simpler version

$datetime = DateTime::createFromFormat('Y-n-j H:i', $DYear.'-'.$DMonth.'-'.$DDay.' '.$DHour.':'.$DMin);
echo $datetime->format('Y-m-d H:i:s'); // Birthday
$datetime->modify('+5 days');
echo $datetime->format('Y-m-d H:i:s'); // 5 days later

Upvotes: 2

Related Questions