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.
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
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"];
Related
Hello everyone one I want to get this kind of drop down menu which has years need to be selected
For example season 2021/2022, 2020/2021, all these value should be generated from 1990 to 2022
If I understood correctly, you want to populate the select with options with all seasons until now since a specific year?
You could modify a for loop so the years appears in a descending order, and then output the option tag with the "previous year / year" as a value like this:
<div class="season-select-wrapper">
<?php
// Define variables for the select
$currentYear = 2022;
$startYear = 1990;
?>
<label for="season">Select a season</label>
<select name="season" id="season">
<?php for ($year = $currentYear; $year > $startYear; $year--) { $prevYear = $year - 1; ?>
<option value="<?php echo "{$prevYear}/{$year}"; ?>"><?php echo "{$prevYear}/{$year}"; ?></option>
<?php } ?>
</select>
</div>
This would create HTML markup like this:
<div class="season-select-wrapper">
<label for="season">Select a season</label>
<select name="season" id="season">
<option value="2021/2022">2021/2022</option>
<option value="2020/2021">2020/2021</option>
<option value="2019/2020">2019/2020</option>
<option value="2018/2019">2018/2019</option>
<option value="2017/2018">2017/2018</option>
<option value="2016/2017">2016/2017</option>
<option value="2015/2016">2015/2016</option>
<option value="2014/2015">2014/2015</option>
<option value="2013/2014">2013/2014</option>
<option value="2012/2013">2012/2013</option>
<option value="2011/2012">2011/2012</option>
<option value="2010/2011">2010/2011</option>
<option value="2009/2010">2009/2010</option>
<option value="2008/2009">2008/2009</option>
<option value="2007/2008">2007/2008</option>
<option value="2006/2007">2006/2007</option>
<option value="2005/2006">2005/2006</option>
<option value="2004/2005">2004/2005</option>
<option value="2003/2004">2003/2004</option>
<option value="2002/2003">2002/2003</option>
<option value="2001/2002">2001/2002</option>
<option value="2000/2001">2000/2001</option>
<option value="1999/2000">1999/2000</option>
<option value="1998/1999">1998/1999</option>
<option value="1997/1998">1997/1998</option>
<option value="1996/1997">1996/1997</option>
<option value="1995/1996">1995/1996</option>
<option value="1994/1995">1994/1995</option>
<option value="1993/1994">1993/1994</option>
<option value="1992/1993">1992/1993</option>
<option value="1991/1992">1991/1992</option>
<option value="1990/1991">1990/1991</option>
</select>
</div>
I have a table with a varchar column. I use a select form with values 01, 02, 03, 04 .... 10, 11, 12 etc to store data in this column. My problem is that, when I submit the form, the leading 'Zero' is removed. How can I maintain the leading zero?
**
NB. I have checked other similar posts but they don't have the
solution for my issue. I have tried var/char/text and still it's not
working hence why I posted this question if there's another way to get this done.
**
<form method=post enctype="multipart/form-data">
<select name="dd">
<option selected="selected">Day</option>
<option value='01'>01</option>
<option value='02'>02</option>
<option value='03'>03</option>
<option value='04'>04</option>
<option value='05'>05</option>
<option value='06'>06</option>
<option value='07'>07</option>
<option value='08'>08</option>
<option value='09'>09</option>
<option value='10'>10</option>
<option value='11'>11</option>
</select>
<select name="mm">
<option selected="selected">Month</option>
<option value='01'>01</option>
<option value='02'>02</option>
<option value='03'>03</option>
<option value='04'>04</option>
<option value='05'>05</option>
<option value='06'>06</option>
<option value='07'>07</option>
<option value='08'>08</option>
<option value='09'>09</option>
<option value='10'>10</option>
<option value='11'>11</option>
</select>
</form>
MySQL is as follows;
$caption = $_POST['dd'].-$_POST['mm'];
$sql = "INSERT INTO pages SET caption = '$caption'";
Use sprintf to format your value:
$caption = sprintf("%02d-%02d", $_POST['dd'], $_POST['mm']);
The key here is to work with the data as strings and don't do math on it like -$_POST['mm'] which will treat it as a number. In numbers leading zeros are always omitted because they always exist. 10 is actually something like 0x0000000a as far as the CPU is concerned.
How to select where month in mysql and php ?
SELECT * FROM `myTable` WHERE insert_date=MONTH($_POST['month']);
Type insert_date = datetime
here code for input post month:
<select name="month">
<option value="" selected>Choose Month :</option>
<option value="1">January</option>
<option value="2">February</option>
<option value="3">Maret</option>
<option value="4">April</option>
<option value="5">Mei</option>
<option value="6">Juny</option>
<option value="7">July</option>
<option value="8">Augustus</option>
<option value="9">September</option>
<option value="10">Oktober</option>
<option value="11">November</option>
<option value="12">Desember</option>
</select>
Query should be like this
SELECT * FROM myTable WHERE MONTH(insert_date)=$_POST['month'];
Do not use $_POST directly. At least use (int) $_POST['month']
If insert_date is a date or timestamp field, use WHERE MONTH(insert_date) = :month with :month bind as (int) $_POST['month']
I am having an issue fetching the values from a dropdown list to check a condition with the mysql query. It is using codeigniter. Let me post the code I have included below:
VIEW:
<select name="month" id="month">
<option>Select Month</option>
<option value="01">January</option>
<option value="02">February</option>
<option value="03">March</option>
<option value="04">April</option>
<option value="05">May</option>
<option value="06">June</option>
<option value="07">July</option>
<option value="08">August</option>
<option value="09">September</option>
<option value="10">October</option>
<option value="11">November</option>
<option value="12">December</option>
</select>
The above values should be equal to the month in the date column of the table. I will post my model below:
MODEL:
$month = $this->input->post('month');
$this->datatables->select('order_no,order_date,naturescrm_customers.name,total,naturescrm_users.username,naturescrm_order.id,
naturescrm_order_status.status_name')
->unset_column('naturescrm_order.id')
->add_column('Actions', btn_edit('agent/order/edit/' . '$1').' '.get_view('$1','agent/order/view/') ,'naturescrm_order.id')
->from($this->_table_name)
->join("naturescrm_customers","naturescrm_customers.id=naturescrm_order.customer_id","left")
->join("naturescrm_order_status","naturescrm_order_status.status_id=naturescrm_order.order_status","left")
->join("naturescrm_users","naturescrm_users.id=naturescrm_order.user_id","left")
->where("DATE_FORMAT(order_date, '%m') = $month");
$this->db->order_by("order_date","desc");
There is my code in the model and in the view, So when I Select a month in the dropdown, the values being fetched in the database should reflect based on the month. Any help will be much appreciated. Thanks
I have a column which name is possession1, in this column I concat the $month.$year, but problem is when I update only $month then $year value is laps but when I update only $year then $month do not laps but $year update the column with old value and new value like,If month is May and old year is 2013, and when I update only year with 2014 then its showing output May20132014, this is my code
<select id="month" name="month" >
<option value="January">January</option>
<option value="February">February</option>
<option value="March">March</option>
<option value="April">April</option>
</select>
<select id="year" name="year">
<option value="2012">2012</option>
<option value="2013">2013</option>
<option value="2014">2014</option>
<option value="2015">2015</option>
<option value="2016">2016</option>
</select>
$month=#$_POST['month'];
$year=#$_POST['year'];
$arr = array($month, $year);
$possession1 = join("", $arr);
$updateSale="update sale set possession='$possession1' where id='$update_id'";
$r=mysql_query($updateSale);
Try this, Use isset to check the post value,
if(isset($_POST['month']) && isset($_POST['year'])){
$month = $_POST['month'];
$year = $_POST['year'];
$possession1 = $month. " ".$year;
$updateSale="update sale set possession='".$possession1."' where id='$update_id'";
$r=mysql_query($updateSale);
}
Correct your query to this--
$updateSale="update sale set possession='".$possession1."' where id='".$update_id."'";